我正在尝试允许我的用户在表单中注册,一旦用户点击“注册”按钮包含2个可以为NULL的外键,将存储数据的表,但是当我尝试提交一个新的注册表单,我得到错误列名称或提供的值的数量与表定义不匹配,我真的不知道是什么问题。我已经搜索了很多解决方案,但没有一个适用于这种情况。任何帮助表示赞赏!
以下是代码:
protected void signup_btn_Click(object sender, EventArgs e)
{
string strcon = "Data Source=(LocalDB)\\MSSQLLocalDB; AttachDbFilename =C:\\Users\\user\\source\\repos\\Test\\Test\\App_Data\\database.mdf; Integrated Security = True";
SqlConnection con = new SqlConnection(strcon);
con.Open();
String command = "INSERT INTO writer VALUES (@writer_Fn, @writer_Ln, @Age, @Username, @Password, @Email)";
SqlCommand com = new SqlCommand(command, con);
com.Parameters.AddWithValue("@writer_Fn", first_box.Text);
com.Parameters.AddWithValue("@writer_Ln", last_box.Text);
com.Parameters.AddWithValue("@Age", age_box.Text);
com.Parameters.AddWithValue("@Username", user_box.Text);
com.Parameters.AddWithValue("@Password", confirm_box.Text);
com.Parameters.AddWithValue("@Email", email_box.Text);
com.ExecuteNonQuery();
}
这是“writer”表的表定义:
CREATE TABLE [dbo].[writer]
(
[writerID] INT IDENTITY (1, 1) NOT NULL,
[writer_Fn] CHAR (15) NOT NULL,
[writer_Ln] CHAR (15) NOT NULL,
[Age] INT NULL,
[Username] VARCHAR (25) NOT NULL,
[Password] VARCHAR (15) NOT NULL,
[Email] VARCHAR (25) NOT NULL,
[tran_FID] INT NULL,
[content_FID] INT NULL,
PRIMARY KEY CLUSTERED ([writerID] ASC),
CONSTRAINT [FK_writer_content]
FOREIGN KEY ([content_FID]) REFERENCES [dbo].[content] ([ContentID]),
CONSTRAINT [FK_writer_transaction]
FOREIGN KEY ([tran_FID]) REFERENCES [dbo].[transaction] ([tranID])
);
答案 0 :(得分:3)
您没有在INSERT语句中提供列名。
它应该是:
INSERT
INTO writer (writer_Fn, writer_Ln, Age, Username, Password, Email)
VALUES (@writer_Fn, @writer_Ln, @Age, @Username, @Password, @Email)