ASP/ c# - Sql连接与更新
本文关键字:连接 更新 Sql ASP | 更新日期: 2023-09-27 17:53:07
sql connectie
string KlasconnectionString = @"DataSource=RICHARD'MSSQLSERVER12;Initial Catalog=MojoGegevens;Integrated Security=True";
// verbinding
SqlDataAdapter DA = new SqlDataAdapter(sql, KlasconnectionString);
DataSet DS = new DataSet();
sql += "update tblOpmaak set Themakleur = 'red'";
SqlConnection conn = new SqlConnection(KlasconnectionString);
conn.Open();
SqlCommand Cmd = new SqlConnection(sql, conn);
Cmd.ExecuteNonQuery();
conn.Close();
它给出了一个错误在"conn"的行:SqlCommand Cmd = new SqlConnection(sql, conn);
通过使用这行代码(SqlCommand Cmd = new SqlConnection(sql, conn);
)你要做的是创建一个新的连接并将其分配给SqlCommand,这样的分配是无效的,不允许的,这就是为什么它抛出错误。您可以使用以下行创建命令:
SqlCommand Cmd = new SqlCommand (sql, conn);
And a Small advice for you;在where子句中指定条件,否则更新将影响所有行(如果要更新所有行,请保留)
您正在使用SqlCommand
。所以你不能用New SqlConnection
。你应该使用New SqlCommand
SqlCommand Cmd = new SqlCommand (sql, conn);
变化
SqlCommand Cmd = new SqlConnection(sql, conn);
到
SqlCommand Cmd = new SqlCommand(sql, conn);
SqlConnection con = new SqlConnection(@"DataSource=RICHARD'MSSQLSERVER12;Initial Catalog=MojoGegevens;Integrated Security=True");
con.Open();
sql += "update tblOpmaak set Themakleur = 'red'";
SqlDataAdapter DA = new SqlDataAdapter(sql, con);
DataSet DS = new DataSet();
SqlCommand Cmd = new SqlConnection(sql, conn);
Cmd.ExecuteNonQuery();
con.Close();