Error:System.Data.SqlClient.SqlException (0x80131904): 必须声明标量变量 "@titleo fcuortesy"。

namespace_13_6{classProgram{staticvoidMain(string[]args){stringconnstring=@"server=YL... namespace _13_6
{
class Program
{
static void Main(string[] args)
{
string connstring = @"server=YLMF-1705221900\SQLEXPRESS;integrated security=true;uid=sa;pwd=dashazi521;database=northwind";
string qry = @"select * from employees where country='UK'";
string ins = @"insert into employees(firstname,lastname,titleofcuortesy,city,country)
values(@firstname,@lastname,@titleofcuortesy,@city,@country)";
SqlConnection conn = new SqlConnection(connstring);
try
{
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand(qry, conn);
DataSet ds = new DataSet();
da.Fill(ds, "employees");
DataTable dt=ds.Tables["employees"];
DataRow newRow = dt.NewRow();
newRow["firstname"] = "Roy";
newRow["lastname"] = "Beatty";
newRow["titleofcourtesy"] = "Sir";
newRow["city"] = "Birmingham";
newRow["country"] = "UK";
dt.Rows.Add(newRow);
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("{0}{1}{2}", row["firstname"].ToString().PadRight(15),
row["lastname"].ToString().PadLeft(25),
row["city"].ToString().PadLeft(20));
}
SqlCommand cmd = new SqlCommand(ins, conn);
cmd.Parameters.Add("@firstname", SqlDbType.NVarChar, 10, "firstname");
cmd.Parameters.Add("@lastname", SqlDbType.NVarChar, 20, "lastname");
cmd.Parameters.Add("@titleofcourtesy", SqlDbType.NVarChar, 25, "titleofcourtesy");
cmd.Parameters.Add("@city",SqlDbType.NVarChar,15,"city");
cmd.Parameters.Add("@country",SqlDbType.NVarChar,15,"country");
da.InsertCommand = cmd;
da.Update(ds,"employees");

}
catch (Exception e)
{
Console.WriteLine("Error:" + e);
}
finally
{
conn.Close();
}
}
}
}
展开
 我来答
百度网友6705c13
2017-07-25 · TA获得超过801个赞
知道小有建树答主
回答量:613
采纳率:83%
帮助的人:193万
展开全部
SQL里写的是@titleofcuortesy,参数提供的是@titleofcourtesy,不一样,你拼错了导致的
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式