3 回答
TA贡献1836条经验 获得超4个赞
bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString())
为您提供方法的重写实现ToString
。这意味着您没有从上面的代码中获得实际值。你应该bunifuCustomDataGrid2.Rows[i].Cells["firstname"].Value
改用。
如果有帮助,请标记为已回答。
TA贡献1851条经验 获得超5个赞
我检查了您的代码并进行了此更改。您可以使用以下代码。
string StrQuery;
try
{
using (SqlConnection conn = new SqlConnection(stringcon))
{
for (int i = 0; i < bunifuCustomDataGrid2.Rows.Count; i++)
{
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
StrQuery = @"INSERT INTO concediati(nume,prenume,idcar,idrent,idclient) VALUES (@name,@lastname,@car,@rent,@client)";
comm.Parameters.AddWithValue("@name", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["firstname"].ToString()));
comm.Parameters.AddWithValue("@lastname", Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["lastname"].ToString()));
comm.Parameters.AddWithValue("@car", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CARS"].ToString()));
comm.Parameters.AddWithValue("@rent", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["RENT"].ToString()));
comm.Parameters.AddWithValue("@client", Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].ToString()));
comm.CommandText = StrQuery;
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
}
}
}
catch (Exception ex)
{
throw;
}
TA贡献1784条经验 获得超9个赞
INSERT的文档在表名和列列表之间显示了一个空格,因此最好遵循该空格。
此外,您可以在循环外仅创建一次参数并在循环中设置它们的值(否则您需要对参数调用 .Clear() 并在每次迭代时重新创建它们):
string sql = @"INSERT INTO concediati (nume, prenume, idcar, idrent, idclient) VALUES (@name, @lastname, @car, @rent, @client)";
using (SqlConnection conn = new SqlConnection(stringcon))
{
using (SqlCommand comm = new SqlCommand(sql, conn))
{
comm.Parameters.Add(new SqlParameter { ParameterName = "@name", SqlDbType = SqlDbType.VarChar, Size = 50 });
comm.Parameters.Add(new SqlParameter { ParameterName = "@lastname", SqlDbType = SqlDbType.VarChar, Size = 50 });
comm.Parameters.Add(new SqlParameter { ParameterName = "@car", SqlDbType = SqlDbType.Int });
comm.Parameters.Add(new SqlParameter { ParameterName = "@rent", SqlDbType = SqlDbType.Int });
comm.Parameters.Add(new SqlParameter { ParameterName = "@client", SqlDbType = SqlDbType.Int });
conn.Open();
for (int i = 0; i < bunifuCustomDataGrid2.Rows.Count; i++)
{
string firstName = Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["firstname"].Value);
string lastName = Convert.ToString(bunifuCustomDataGrid2.Rows[i].Cells["lastname"].Value);
int car = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CARS"].Value);
int rent = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["RENT"].Value);
int client = Convert.ToInt32(bunifuCustomDataGrid2.Rows[i].Cells["CLIENT"].Value);
comm.Parameters["@name"].Value = firstName;
comm.Parameters["@lastname"].Value = lastName;
comm.Parameters["@car"].Value = car;
comm.Parameters["@rent"].Value = rent;
comm.Parameters["@client"].Value = client;
comm.ExecuteNonQuery();
}
}
}
- 3 回答
- 0 关注
- 87 浏览
添加回答
举报