求一段代码,执行存储过程,类似于
public DataSet GetDataSet(string selectCmd) // Select From Table
{
DataSet ds = null;
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlDataAdapter m_SqlDataAdapter = new SqlDataAdapter(selectCmd,m_SqlConnection);
try
{
ds = new DataSet();
m_SqlDataAdapter.Fill(ds,"Table0");
} // end try
catch (Exception e)
{
throw new Exception("Error in CCPUCode:GetDataSet()-> " + e.ToString());
}
finally
{
m_SqlDataAdapter.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return ds;
} // end GetDataSet
要能输入参数的那种
public DataSet GetDataSet(string selectCmd) // Select From Table
{
DataSet ds = null;
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlDataAdapter m_SqlDataAdapter = new SqlDataAdapter();
m_SqlDataAdapter.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
m_SqlDataAdapter.SelectCommand.CommandText = "存储过程名";
m_SqlDataAdapter.SelectCommand.Connection = m_SqlConnection;
try
{
ds = new DataSet();
m_SqlDataAdapter.Fill(ds,"Table0");
} // end try
catch (Exception e)
{
throw new Exception("Error in CCPUCode:GetDataSet()-> " + e.ToString());
}
finally
{
m_SqlDataAdapter.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return ds;
} // end GetDataSet
public DataSet GetDataSet(string 存储过程名) // Select From Table
{
DataSet ds = null;
SqlConnection m_SqlConnection = new SqlConnection(m_dbConnection);
SqlDataAdapter m_SqlDataAdapter = new SqlDataAdapter();
m_SqlDataAdapter.SelectCommand.CommandType = System.Data.CommandType.StoredProcedure;
m_SqlDataAdapter.SelectCommand.CommandText = "存储过程名";
m_SqlDataAdapter.SelectCommand.Connection = m_SqlConnection;
try
{
ds = new DataSet();
m_SqlDataAdapter.Fill(ds,"Table0");
} // end try
catch (Exception e)
{
throw new Exception("Error in CCPUCode:GetDataSet()-> " + e.ToString());
}
finally
{
m_SqlDataAdapter.Dispose();
m_SqlConnection.Close();
m_SqlConnection.Dispose();
}
return ds;
} // end GetDataSet
新增
public bool InsertComInfo(int userid,int classid,string company,string phone,string fax,string contact,string handset,string address,
string zipcode,string email,string homepage,string imageurl)
{
SqlCommand cmd=new SqlCommand("INSERTCOM_INFO",con);
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter parID=new SqlParameter("@userid",SqlDbType.Int);
parID.Value=userid;
SqlParameter parClassID=new SqlParameter("@classid",SqlDbType.Int);
parClassID.Value=classid;
SqlParameter parCompany=new SqlParameter("@company",SqlDbType.VarChar,30);
parCompany.Value=company;
SqlParameter parPhone=new SqlParameter("@phone",SqlDbType.VarChar,20);
parPhone.Value=phone;
SqlParameter parFax=new SqlParameter("@fax",SqlDbType.VarChar,20);
parFax.Value=fax;
SqlParameter parContact=new SqlParameter("@contact",SqlDbType.VarChar,30);
parContact.Value=contact;
SqlParameter parHandSet=new SqlParameter("@handset",SqlDbType.VarChar,20);
parHandSet.Value=handset;
SqlParameter parAddress=new SqlParameter("@address",SqlDbType.VarChar,50);
parAddress.Value=address;
SqlParameter parZipCode=new SqlParameter("@zipcode",SqlDbType.VarChar,10);
parZipCode.Value=zipcode;
SqlParameter parEmail=new SqlParameter("@email",SqlDbType.VarChar,30);
parEmail.Value=email;
SqlParameter parHomePage=new SqlParameter("@homepage",SqlDbType.VarChar,30);
parHomePage.Value=homepage;
SqlParameter parImageUrl=new SqlParameter("@imageurl",SqlDbType.VarChar,70);
parImageUrl.Value=imageurl;
cmd.Parameters.Add(parID);
cmd.Parameters.Add(parClassID);
cmd.Parameters.Add(parCompany);
cmd.Parameters.Add(parPhone);
cmd.Parameters.Add(parFax);
cmd.Parameters.Add(parContact);
cmd.Parameters.Add(parHandSet);
cmd.Parameters.Add(parAddress);
cmd.Parameters.Add(parZipCode);
cmd.Parameters.Add(parEmail);
cmd.Parameters.Add(parHomePage);
cmd.Parameters.Add(parImageUrl);
con.Open();
int result=cmd.ExecuteNonQuery();
con.Close();
if(result>0)
{
return true;
}
else
{
return false;
}
}
返回DataSet
public DataSet GetComInfoByID(int classid)
{
SqlCommand cmd=new SqlCommand("COM_INFOBYID",con);
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter parClassID=new SqlParameter("@classid",SqlDbType.Int);
parClassID.Value=classid;
cmd.Parameters.Add(parClassID);
cmdAdp.SelectCommand=cmd;
DataSet ds=new DataSet();
con.Open();
cmdAdp.Fill(ds);
con.Close();
return ds;
}
我上面的连接字符串没有写
private string conStr;
private SqlConnection con;
private SqlDataAdapter cmdAdp;
public ComInfo()
{
//
// TODO: 在此处添加构造函数逻辑
//
conStr=System.Configuration.ConfigurationSettings.AppSettings["conn"].ToString();
con=new SqlConnection(conStr);
cmdAdp=new SqlDataAdapter();
}