C#连接SQL 插入语句! 急!高手来!!!

来源:百度知道 编辑:UC知道 时间:2024/06/17 03:51:22
string connectionString = @"server=localhost;User ID=sa;Password=111111;database=test";

SqlConnection myConnection = new SqlConnection(connectionString);

myConnection.Open();
string Insert = "insert into Table1 where 1=1";
if (TextBox1.Text.Trim().Equals("") == false)
{
Insert += " and name = "(" + txt_Name.text + ");
}
if (TextBox2.Text.Trim().Equals("") == false)
{
Insert += " and Ege = "(" + txt_Ege.text + ");
}
if (TextBox5.Text.Trim().Equals("") == false)
{
Insert += " and Biithday = "("+ txt_Biithday.text + ");
}
test.ExecuteNonQuery();
myConnection.Close();

根据你说的情况,你想插入你想要的textBox中的值的话,如下代码:
string connectionString = @"server=localhost;User ID=sa;Password=111111;database=test";

SqlConnection myConnection = new SqlConnection(connectionString);
myConnection.Open();
string Insert = "insert into Table1 values('";
Insert += txt_Name.text + "',";
Insert += txt_Ege.text + ",";
Insert += txt_Biithday.text + ")";
SqlCommand sqlCmd = new SqlCommand(Insert,myConnection);
sqlCmd.ExecuteNonQuery();
myConnection.Close();

注:Table1中的字段(列)要对应values中要插入的值的个数和Sql数据类型~简单说:就是你Table1中有名字,年龄和出生日期,就要一一对应要插入的值!不能多不能少,在SQL中是字符串要用''括起~

个人给阁下的建议:
insert,update,select,delete是Sql语句中的基本语法~你最好是去学点SQL的基础先~
还有:
if (TextBox5.Text.Trim().Equals("") == false)
{
Insert += " and Biithday = "("+ txt_Biithday.text + ");
}
这是什么意思,TextBox5就是txt_Biithday吗?在后台中要调