在c#中插入数据,ID需要自增加,再代码中如何做呢?试了好几个都不行,
这个是建立表的的过程
string sqlMessage = null;
sqlMessage = "use Celou " + "create table information_demo" + "(ID INT,d_Time datetime,d_ProductType varchar(255),d_Code varchar(255),d_Leaknum varchar(255), d_Pressure varchar(255), d_Leakvalue varchar(255),d_Leakresult varchar(255))";
SqlCommand Scmd = new SqlCommand(sqlMessage, conn);
这个插入数据
SqlConnection conn = new SqlConnection("Data Source = admin-PC; Database = Celou;User Id = sa; Password = 123456");
bool res = false;
try
{
//打开数据库
conn.Open();
//实例化命令对象
SqlCommand myCommand = new SqlCommand();
//把要操作的数据库传过来
myCommand.Connection = conn;
//操作类型为文本类型
myCommand.CommandType = CommandType.Text;
//
myCommand.CommandText = @"insert into information_demo(ID,d_Time,d_ProductType,d_Code,d_Leaknum,d_Pressure,d_Leakvalue,d_Leakresult) values(@ID,@时间,@产品型号,@二维码,@组号,@测试压力,@测试值,@测试结果);select @@identity";
int id = myCommand.ExecuteNonQuery();
string getid = id.ToString("");
DateTime dt = DateTime.Now;
string getdt = dt.ToString("yyyy-MM-dd HH:mm:ss");
myCommand.Parameters.Add(new SqlParameter("@ID", textBox7.Text));
myCommand.Parameters.Add(new SqlParameter("@时间", dt));
myCommand.Parameters.Add(new SqlParameter("@产品型号", textBox6.Text));
myCommand.Parameters.Add(new SqlParameter("@二维码", textBox1.Text));
myCommand.Parameters.Add(new SqlParameter("@组号", textBox2.Text));
myCommand.Parameters.Add(new SqlParameter("@测试压力", textBox3.Text));
myCommand.Parameters.Add(new SqlParameter("@测试值", textBox4.Text));
myCommand.Parameters.Add(new SqlParameter("@测试结果", textBox5.Text));
//提交数据
myCommand.ExecuteNonQuery();
//关闭数据库
conn.Close();
res = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
conn.Close();
}
if (res)
{
MessageBox.Show("添加成功");
}