gpt4 book ai didi

c# - 将值插入选定列的问题

转载 作者:行者123 更新时间:2023-11-30 22:51:54 24 4
gpt4 key购买 nike

好的,所以我已经为此工作了很长一段时间,但仍未找到解决方案。我希望你们能帮助我。我有这个名为 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 绑定(bind)了 DataTable,所以这不是问题。反正我不这么认为。当我尝试插入时,内容如下:

Column name or number of supplied values does not match table definition.

我们将不胜感激任何形式的帮助。

最佳答案

当您在 INSERT INTO 语法中省略列名时,您需要为所有列提供值,而不仅仅是其中的一个子集。

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

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

关于c# - 将值插入选定列的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27947138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com