web.config:
<connectionStrings>
<add name="conn" connectionString="Data Source=localhost;Initial Catalog=New;User ID=sdd;Password=Sddfy24"/>
</connectionStrings>
DbHelper:
public static readonly string conn = ConfigurationManager.ConnectionStrings["conn"].ToString();
public void GetSqlOperateResult(string procName, params SqlParameter[] parameter)
{
SqlConnection myconn = null;
using (myconn = new SqlConnection(conn))
{
try
{
if (myconn.State != ConnectionState.Open)
{
myconn.Open();
}
SqlCommand comm = myconn.CreateCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = procName;
foreach (SqlParameter parm in parameter)
{
comm.Parameters.Add(parm);
}
comm.ExecuteNonQuery();
myconn.Close();
}
catch
{
myconn.Close();
}
}
}
public DataSet GetSqlOperateDataset(string procName, params SqlParameter[] parameter)
{
SqlConnection myconn = null;
DataSet ds = new DataSet();
using (myconn = new SqlConnection(conn))
{
try
{
if (myconn.State != ConnectionState.Open)
{
myconn.Open();
}
SqlCommand comm = myconn.CreateCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = procName;
if (parameter != null)
{
foreach (SqlParameter para in parameter)
{
comm.Parameters.Add(para);
}
}
SqlDataAdapter ada = new SqlDataAdapter(comm);
ada.Fill(ds);
myconn.Close();
}
catch
{
myconn.Close();
}
}
return ds;
}
事件处理:
protected void btnSave_Click(object sender, EventArgs e)
{
string title = this.txtTitle.Text.Trim();
string content = this.txtContent.Text.Trim();
SqlParameter parameterTitle = new SqlParameter("@Title", title);
SqlParameter parameterContent = new SqlParameter("@Content", content);
SqlParameter[] para = { parameterTitle, parameterContent };
string procName="InsertNews";
DbHelper db = new DbHelper();
db.GetSqlOperateResult(procName,para);
}
<connectionStrings>
<add name="conn" connectionString="Data Source=localhost;Initial Catalog=New;User ID=sdd;Password=Sddfy24"/>
</connectionStrings>
DbHelper:
public static readonly string conn = ConfigurationManager.ConnectionStrings["conn"].ToString();
public void GetSqlOperateResult(string procName, params SqlParameter[] parameter)
{
SqlConnection myconn = null;
using (myconn = new SqlConnection(conn))
{
try
{
if (myconn.State != ConnectionState.Open)
{
myconn.Open();
}
SqlCommand comm = myconn.CreateCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = procName;
foreach (SqlParameter parm in parameter)
{
comm.Parameters.Add(parm);
}
comm.ExecuteNonQuery();
myconn.Close();
}
catch
{
myconn.Close();
}
}
}
public DataSet GetSqlOperateDataset(string procName, params SqlParameter[] parameter)
{
SqlConnection myconn = null;
DataSet ds = new DataSet();
using (myconn = new SqlConnection(conn))
{
try
{
if (myconn.State != ConnectionState.Open)
{
myconn.Open();
}
SqlCommand comm = myconn.CreateCommand();
comm.CommandType = CommandType.StoredProcedure;
comm.CommandText = procName;
if (parameter != null)
{
foreach (SqlParameter para in parameter)
{
comm.Parameters.Add(para);
}
}
SqlDataAdapter ada = new SqlDataAdapter(comm);
ada.Fill(ds);
myconn.Close();
}
catch
{
myconn.Close();
}
}
return ds;
}
事件处理:
protected void btnSave_Click(object sender, EventArgs e)
{
string title = this.txtTitle.Text.Trim();
string content = this.txtContent.Text.Trim();
SqlParameter parameterTitle = new SqlParameter("@Title", title);
SqlParameter parameterContent = new SqlParameter("@Content", content);
SqlParameter[] para = { parameterTitle, parameterContent };
string procName="InsertNews";
DbHelper db = new DbHelper();
db.GetSqlOperateResult(procName,para);
}