string queryString1 = " create table tbtest";
queryString1 += " (testid int primary key identity(1,1) ,";
queryString1 += " testcount int not null);go;";
queryString1 += " insert into tbtest(testcount) values(30) "; SqlConnection MyConnection1 = new SqlConnection(strcon);
SqlCommand MyCommand1 = new SqlCommand(queryString1, MyConnection1);
MyConnection1.Open();
MyCommand1.ExecuteNonQuery();
MyConnection1.Close();
queryString1 += " (testid int primary key identity(1,1) ,";
queryString1 += " testcount int not null);go;";
queryString1 += " insert into tbtest(testcount) values(30) "; SqlConnection MyConnection1 = new SqlConnection(strcon);
SqlCommand MyCommand1 = new SqlCommand(queryString1, MyConnection1);
MyConnection1.Open();
MyCommand1.ExecuteNonQuery();
MyConnection1.Close();
你在sql查询分析器中写完建表sql,后面也应该有一个go啊
然后才可插入啊,否则会提示你错误的
{
string strcon = @"server=.\sqlexpress;uid=sa;pwd=;database=demo";
string queryString1 = "create table tbtest";
queryString1 += " (testid int primary key identity(1,1) ,";
queryString1 += " testcount int not null);";
queryString1 += " insert into tbtest(testcount) values(30) "; SqlConnection MyConnection1 = new SqlConnection(strcon);
SqlCommand MyCommand1 = new SqlCommand(queryString1, MyConnection1);
MyConnection1.Open();
MyCommand1.ExecuteNonQuery();
MyConnection1.Close();
}数据库里表名是小写,而且也插入记录成功