protected void Button1_Click(object sender, EventArgs e) { string constring="Data Source=.;Initial Catalog=数据库名;Integrated Security=True"; using (SqlConnection con = new SqlConnection(constring)) { con.Open(); string sql = "insert into 表名(列) values(1)"; SqlCommand cmd = new SqlCommand(sql, con); int intStr = cmd.ExecuteNonQuery(); if (intStr > 0) { Response.Write("插入成功"); } else { Response.Write("失败"); } } } else { Response.Write("失败"); }
楼上ok,不过注意try catch捕获异常
try { con.Open(); SqlCommand cmd = new SqlCommand("Select * FROM 表名", con); SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd; ds = new DataSet(); dt = new DataTable(); sda.Fill(ds); dt = ds.Tables[0]; if(ds.Tables[0].Rows.Count>0) { Messagebox.Show("成功"); } } catch (Exception ex) { MessageBox.Show("报错原因:" + ex.Message); return; }
以上代码写在botton的单击事件里面
在这个基础上加上try{}catch{}就行了,你懂的
string constring="Data Source=.;Initial Catalog=数据库名;Integrated Security=True"; using (SqlConnection con = new SqlConnection(constring)) con.Open(); SqlCommand cmd = new SqlCommand("Select * FROM 表名", con); SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd; ds = new DataSet(); dt = new DataTable(); sda.Fill(ds);打包放着也行 这杨每次用都要写
{
string constring="Data Source=.;Initial Catalog=数据库名;Integrated Security=True";
using (SqlConnection con = new SqlConnection(constring))
{ con.Open();
string sql = "insert into 表名(列) values(1)";
SqlCommand cmd = new SqlCommand(sql, con);
int intStr = cmd.ExecuteNonQuery();
if (intStr > 0)
{
Response.Write("插入成功");
}
else
{
Response.Write("失败");
}
} }
else
{
Response.Write("失败");
}
{
con.Open();
SqlCommand cmd = new SqlCommand("Select * FROM 表名", con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
ds = new DataSet();
dt = new DataTable();
sda.Fill(ds); dt = ds.Tables[0];
if(ds.Tables[0].Rows.Count>0)
{
Messagebox.Show("成功");
}
}
catch (Exception ex)
{
MessageBox.Show("报错原因:" + ex.Message);
return;
}
在这个基础上加上try{}catch{}就行了,你懂的
using (SqlConnection con = new SqlConnection(constring)) con.Open();
SqlCommand cmd = new SqlCommand("Select * FROM 表名", con);
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
ds = new DataSet();
dt = new DataTable();
sda.Fill(ds);打包放着也行 这杨每次用都要写