报必须声明标量变量 "@Number"错误,各位大侠帮忙看一下
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Insert into T_number(number,promary,city,name) values(@Number,@Promary,@City,@Name)";
foreach (string file in files)
{
string[] lines = File.ReadAllLines(file, Encoding.Default);
foreach (string line in lines)
{
string[] strs = line.Split(' ');
string Number=strs[0];
string Promary=strs[1];
string City=strs[2];
string Name = strs[3];
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("nubmer", Number));
cmd.Parameters.Add(new SqlParameter("promary", Promary));
cmd.Parameters.Add(new SqlParameter("city", City));
cmd.Parameters.Add(new SqlParameter("name", Name));
cmd.ExecuteNonQuery();
}
}
}
}
[解决办法]
cmd.Parameters.Add(new SqlParameter("@Number", Number));
cmd.Parameters.Add(new SqlParameter("@Promary", Promary));
cmd.Parameters.Add(new SqlParameter("@City", City));
cmd.Parameters.Add(new SqlParameter("@Name", Name));
[解决办法]
cmd.Parameters.Add(new SqlParameter("@Number", Number));
要和你sql中的一致
[解决办法]
参数要对应,
cmd.Parameters.Add(new SqlParameter("@Number", Number));
cmd.Parameters.Add(new SqlParameter("@Promary", Promary));
cmd.Parameters.Add(new SqlParameter("@City", City));
cmd.Parameters.Add(new SqlParameter("@Name", Name));
[解决办法]
我改成一致的还是报这个错
改后代码
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Insert into T_number(number,promary,city,name) values(@number,@nromary,@nity,@name)";
foreach (string file in files)
{
string[] lines = File.ReadAllLines(file, Encoding.Default);
foreach (string line in lines)
{
string[] strs = line.Split(' ');
string number=strs[0];
string promary=strs[1];
string city=strs[2];
string name = strs[3];
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@nubmer", number));
cmd.Parameters.Add(new SqlParameter("@promary", promary));
cmd.Parameters.Add(new SqlParameter("@city", city));
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.ExecuteNonQuery();
}
}
}
}
[解决办法]
数据库里的也一样全是小写
数据库信息
id int
number nvarchar(50)
promary nvarchar(50)
city nvarchar(50)
name nvarchar(50)
[解决办法]
cmd.CommandText = "Insert into T_number(number,promary,city,name) values(@number,@nromary,@nity,@name)";
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@nubmer", number));
cmd.Parameters.Add(new SqlParameter("@promary", promary));
cmd.Parameters.Add(new SqlParameter("@city", city));
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.ExecuteNonQuery();
红色部分写错了
[解决办法]
cmd.CommandText = "Insert into T_number(number,promary,city,name) values(@number,@nromary,@nity,@name)";
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@nubmer", number));
cmd.Parameters.Add(new SqlParameter("@promary", promary));
cmd.Parameters.Add(new SqlParameter("@city", city));
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.ExecuteNonQuery();
红色部分写错了,要对应
[解决办法]
using (SqlConnection conn = new SqlConnection(connstr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "Insert into T_number(number,promary,city,name) values(@number,@promary,@city,@name)";
foreach (string file in files)
{
string[] lines = File.ReadAllLines(file, Encoding.Default);
foreach (string line in lines)
{
string[] strs = line.Split(' ');
string number=strs[0];
string promary=strs[1];
string city=strs[2];
string name = strs[3];
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@nubmer", number));
cmd.Parameters.Add(new SqlParameter("@promary", promary));
cmd.Parameters.Add(new SqlParameter("@city", city));
cmd.Parameters.Add(new SqlParameter("@name", name));
cmd.ExecuteNonQuery();
}
}
}
}
还是报 必须声明标量变量 "@number"。 错误