将值插入到选定的列中会出现问题

时间:2015-01-14 15:56:52

标签: c# mysql asp.net

好的,所以我已经在这方面工作了很长一段时间,但还没有找到解决方案。我希望你们能帮助我。 我有一个名为Cardiology的数据库表:

[CID]                 INT           IDENTITY (1, 1) NOT NULL,
[Name]                NVARCHAR (50) NOT NULL,
[Designation]         TEXT          NOT NULL,
[Qualification]       NVARCHAR (50) NULL,
[Shift]               NVARCHAR (50) NOT NULL,
[Appointment_Timings] NVARCHAR (50) NOT NULL,
[Ward_rounds]         NVARCHAR (50) NULL,
[Slot1]               NVARCHAR (50) NULL,
[Slot2]               NVARCHAR (50) NULL,
[Slot3]               NVARCHAR (50) NULL,
[Slot4]               NVARCHAR (50) NULL,
[BreakTime]           NVARCHAR (50) NULL,
[Slot5]               NVARCHAR (50) NULL,
[Slot6]               NVARCHAR (50) NULL,
[Slot7]               NVARCHAR (50) NULL,
[Slot8]               NVARCHAR (50) NULL,
CONSTRAINT [PK_Cardiology] PRIMARY KEY CLUSTERED ([CID] ASC)

我想仅将数据输入几列,我的查询是:

string str = "INSERT INTO Cardiology values (@Name,@Designation,@Qualification,@Shift,@Appointment_Timings)";
            cmd = new SqlCommand(str, con);
            cmd.Parameters.AddWithValue("@Name", TextBox1.Text);
            cmd.Parameters.AddWithValue("@Designation", DropDownList1.Text);
            cmd.Parameters.AddWithValue("@Qualification", TextBox3.Text);
            cmd.Parameters.AddWithValue("@Shift", DropDownList2.Text);
            cmd.Parameters.AddWithValue("@Appointment_Timings", DropDownList3.Text);
            con.Open();
            int flag = cmd.ExecuteNonQuery();
            if (flag == 1)    //On successful updation, shows a popup message
            {
                string msg = "Operation Successful";
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("<script type = 'text/javascript'>");
                sb.Append("window.onload=function(){");
                sb.Append("alert('");
                sb.Append(msg);
                sb.Append("')};");
                sb.Append("</script>");
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb.ToString());
            }
            else if (flag == 0)
            {
                string msg1 = "Operation Unsuccessful";
                System.Text.StringBuilder sb1 = new System.Text.StringBuilder();
                sb1.Append("<script type = 'text/javascript'>");
                sb1.Append("window.onload=function(){");
                sb1.Append("alert('");
                sb1.Append(msg1);
                sb1.Append("')};");
                sb1.Append("</script>");
                ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", sb1.ToString());
            }
            show1();
            con.Close();

show1()是这样的:

public void show1()
    {
        da = new SqlDataAdapter("SELECT [CID], [Name], [Designation], [Qualification], [Shift], [Appointment_Timings] FROM [Cardiology]", con);
        da.Fill(ds);
        GridView1.DataBind();
    }

我通过GridView绑定了DataTable,这不是问题。反正我也不这么认为。 当我尝试插入时,这就是说:

  

列名或提供的值与表定义不匹配。

非常感谢任何形式的帮助。

3 个答案:

答案 0 :(得分:0)

当您省略INSERT INTO语法中的列名时,您需要为所有列提供值,而不仅仅是它们的一部分。

您需要更改INSERT INTO命令文本以指定要接收参数传入的值的列的名称

 string str = @"INSERT INTO Cardiology 
                (Name, Designation,Qualification,Shift, Appointment_Timings) 
                values  
                (@Name,@Designation,@Qualification,@Shift,@Appointment_Timings)";

答案 1 :(得分:0)

您需要提供要插入的列名。

string str = "INSERT INTO Cardiology (Name, Designation, Qualification, Shift, Appointment_Timings) values (@Name,@Designation,@Qualification,@Shift,@Appointment_Timings)";

答案 2 :(得分:0)

如果您未在所有列中插入值,则需要指定要输入数据的列,例如插入心脏病学(列)值(值)