日期:2014-05-18 浏览次数:20772 次
// Update the demographics for a store, which is stored // in an xml column. string commandText = "UPDATE Sales.Store SET Demographics = @demographics " + "WHERE CustomerID = @ID;"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(commandText, connection); command.Parameters.Add("@ID", SqlDbType.Int); command.Parameters["@ID"].Value = customerID; // Use AddWithValue to assign Demographics. // SQL Server will implicitly convert strings into XML. command.Parameters.AddWithValue("@demographics", demoXml); try { connection.Open(); Int32 rowsAffected = command.ExecuteNonQuery(); Console.WriteLine("RowsAffected: {0}", rowsAffected); } catch (Exception ex) { Console.WriteLine(ex.Message); } }
------解决方案--------------------
@是个特殊标记,表示后面跟的是sql参数名,既然有了sql参数名,就要添加sql参数值,cmd.Parameters.Add 方法可以添加一个sql参数值,方法里先指定sql参数名也就是@id,再指定类型,同时这个方法返回创建好的 SqlParameter 实例,对这个实例可以赋sql参数具体值,简便写法就可以直接点Value等于什么什么