将datagridview的阿拉伯数据保存在数据库中

时间:2019-03-04 19:35:05

标签: c# sql sql-server database-design

我有datagridview,其中包含两列(名称,年龄)。该gridview的数据为阿拉伯语。 (((无法将参数值从字符串转换为Int32。))) 她是我的密码

SqlConnection connection = new SqlConnection("Server=addie; DataBase=Library_DB; Integrated Security=true");
       connection.Open();
       SqlCommand command = new SqlCommand("INSERT INTO names (name,age) VALUES (@Name, @Age)", connection);
       command.Parameters.Add("@Name", SqlDbType.Int);
       command.Parameters.Add("@Age", SqlDbType.NVarChar, 50); //size and type must match your DB

       for (int i = 0; i < dataGridView1.Rows.Count; i++)
       {
           command.Parameters["@Name"].Value = dataGridView1.Rows[i].Cells[0].Value;
           command.Parameters["@Age"].Value = dataGridView1.Rows[i].Cells[1].ValueType;
           command.ExecuteNonQuery();
       }
       connection.Close();
       dataGridView1.Rows.Clear();

当我尝试运行代码时,出现以下错误

  

无法将参数值从字符串转换为Int32。

如何修复我的代码?

1 个答案:

答案 0 :(得分:0)

嗨,如果我了解您的墙,我认为您在.net代码中输入了错误,请尝试以下操作:

SqlConnection connection = new SqlConnection("Server=addie; DataBase=Library_DB; Integrated Security=true");
       connection.Open();
       SqlCommand command = new SqlCommand("INSERT INTO names (name,age) VALUES (@Name, @Age)", connection);
       command.Parameters.Add("@Age", SqlDbType.Int);
       command.Parameters.Add("@Name", SqlDbType.NVarChar, 50); //size and type must match your DB

       for (int i = 0; i < dataGridView1.Rows.Count; i++)
       {
           command.Parameters["@Name"].Value = dataGridView1.Rows[i].Cells[0].Value;
           command.Parameters["@Age"].Value = dataGridView1.Rows[i].Cells[1].ValueType;
           command.ExecuteNonQuery();
       }
       connection.Close();
       dataGridView1.Rows.Clear();

您有刷卡名称类型和年龄类型。

否则,如果它不起作用,我们也非常了解您的数据库整理版本和其他一些信息。

相关问题