数据库表user如图
代码如下:
this.Open();
string cmdString="INSERT INTO user(ID,Frist,Second,Third,Fourth,Fifth,Sixth,Blue,Name) VALUES ('3000001','1','2','3','4','5','6','10','name')";
SqlCommand cmd=new SqlCommand(cmdString,con);
cmd.ExecuteNonQuery();
this.Close();
现运行如下如下错误:数据库已能连接上,求问解决办法。
代码如下:
this.Open();
string cmdString="INSERT INTO user(ID,Frist,Second,Third,Fourth,Fifth,Sixth,Blue,Name) VALUES ('3000001','1','2','3','4','5','6','10','name')";
SqlCommand cmd=new SqlCommand(cmdString,con);
cmd.ExecuteNonQuery();
this.Close();
现运行如下如下错误:数据库已能连接上,求问解决办法。
定义con来连接数据库,把this换成con试试
SqlConnection conn = new SqlConnection("连接数据库字符串");
conn.open();
string sqlStr = "T-SQL语句"
SqlCommand cmd = new SqlCommand(sqlStr,conn);
cmd.ExecuteNonQuery();
conn.close();
con.Open();
string cmdString="INSERT INTO user(ID,Frist,Second,Third,Fourth,Fifth,Sixth,Blue,Name) VALUES ('3000001','1','2','3','4','5','6','10','name')";
SqlCommand cmd=new SqlCommand(cmdString,con);
cmd.ExecuteNonQuery();
con.Close();
]#region OPEN
public void Open()
{
if(con==null)
{
string connectionString="Server=Localhost;database=DB_Ticket;uid=sa;pwd=123";
con=new SqlConnection(connectionString);
}
if(con.State==System.Data.ConnectionState.Closed)
con.Open();
Console.WriteLine("数据库状态为"+con.State.ToString());
}
#endregion
Close()方法
#region CLOSE
public void Close()
{
if(con!=null)
{
con.Close();
}
}
#endregion ,数据库连接是没有问题的,con.State.ToString()为Open,在写入数据库的时候报错。ID Varchar(50)类型,‘30000001’这种数据类型不对?报错代码如下:调试器抛出异常System.Data.SqlClient.SqlException:
关键字 'user' 附近有语法错误。RunExecuteNonQueryTds()
InternalExecuteNonQuery()
ExecuteNonQuery()