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();
}
}
}
} 展开
{
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();
}
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询