本文介绍了类型为“System.Data.SqlClient.SqlException”的未处理异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误:



I get this error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: The variable name '@Id' has already been declared. Variable names must be unique within a query batch or stored procedure





我的代码:



my code:

private void button2_Click(object sender, EventArgs e)
{
    string con = "Data Source=dqq5ndqef2.database.windows.net;Initial Catalog=Login;Integrated Security=False;User ID=richardjacobs97;Password=********;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False;";
    string connectionString = con;

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand("INSERT INTO Rota (Id, Name, DateWorking) Values (@identification, @fullname, @dateworking)");
        cmd.CommandType = CommandType.Text;
        cmd.Connection = connection;
        SqlConnectionStringBuilder scb = new SqlConnectionStringBuilder();
        DataTable dt = new DataTable();
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            cmd.Parameters.AddWithValue("@identification", dataGridView1.Rows[i].Cells["Id"].Value);
            cmd.Parameters.AddWithValue("@fullname", dataGridView1.Rows[i].Cells["Name"].Value);
            cmd.Parameters.AddWithValue("@dateworking", dataGridView1.Rows[i].Cells["DateWorking"].Value);
        }
        dt = new DataTable();
        sda.Fill(dt);
        dataGridView1.DataSource = dt;
    }
}





in sugguestions?



in sugguestions?

推荐答案

  for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            cmd.Parameters.AddWithValue("@identification", dataGridView1.Rows[i].Cells["Id"].Value);
            cmd.Parameters.AddWithValue("@fullname", dataGridView1.Rows[i].Cells["Name"].Value);
            cmd.Parameters.AddWithValue("@dateworking", dataGridView1.Rows[i].Cells["DateWorking"].Value);

// Here you need to execute the command
        }





看看这个简单地插入和填充行的解决方案...



[]



private void button2_Click(object sender, EventArgs e)
{
    const string con = "....";

    using (SqlConnection connection = new SqlConnection(con))
    {
        connection.Open();

        using (SqlTransaction transaction = connection.BeginTransaction())
        using (SqlCommand cmd = new SqlCommand("INSERT INTO Rota (Id, Name, DateWorking) Values (@identification, @fullname, @dateworking)", connection, transaction))
        {
            cmd.CommandType = CommandType.Text;

            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                cmd.Parameters.Clear();

                cmd.Parameters.AddWithValue("@identification", dataGridView1.Rows[i].Cells["Id"].Value);
                cmd.Parameters.AddWithValue("@fullname", dataGridView1.Rows[i].Cells["Name"].Value);
                cmd.Parameters.AddWithValue("@dateworking", dataGridView1.Rows[i].Cells["DateWorking"].Value);

                cmd.ExecuteNonQuery();
            }

            transaction.Commit();
        }

        using (SqlCommand cmd = new SqlCommand("SELECT Id, Name, DateWorking FROM Rota", connection))
        {
            DataTable dt = new DataTable();
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
        }
    }
}


这篇关于类型为“System.Data.SqlClient.SqlException”的未处理异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 02:46