SQL插入导致空字段

Gmo*_*ake 2 c# sql asp.net

我正在尝试将文本框中的数据输入到SQL数据库中,当我这样做时,它只插入空字段.

我的代码:

 sqlCommand = new SqlCommand("INSERT INTO Department (Description, Comments, Permissions, Active, Production) VALUES (@description, @comments, @permissions, @active, @production)", sqlConnection);

 sqlCommand.Parameters.Add("@description", SqlDbType.NVarChar, 50);
 sqlCommand.Parameters.Add("@comments", SqlDbType.NVarChar, 50);
 sqlCommand.Parameters.Add("@permissions", SqlDbType.NVarChar, 50);
 sqlCommand.Parameters.Add("@active", SqlDbType.Int);
 sqlCommand.Parameters.Add("@production", SqlDbType.Int);

 sqlCommand.Parameters.AddWithValue("@description", txtDescription.Text);
 sqlCommand.Parameters.AddWithValue("@comments", txtComments.Text);
 sqlCommand.Parameters.AddWithValue("@permissions", txtPermissions.Text);
 sqlCommand.Parameters.AddWithValue("@active", Convert.ToString(Convert.ToUInt32(chkActif.Checked)));
 sqlCommand.Parameters.AddWithValue("@production", Convert.ToString(Convert.ToUInt32(chkProduction.Checked)));
 sqlCommand.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)

sca*_*tag 7

您要添加两次参数!将您的代码更改为.

sqlCommand = new SqlCommand("INSERT INTO Department (Description, Comments, Permissions, Active, Production) VALUES (@description, @comments, @permissions, @active, @production)", sqlConnection);

                sqlCommand.Parameters.AddWithValue("@description", txtDescription.Text);
                sqlCommand.Parameters.AddWithValue("@comments", txtComments.Text);
                sqlCommand.Parameters.AddWithValue("@permissions", txtPermissions.Text);
                sqlCommand.Parameters.AddWithValue("@active", Convert.ToString(Convert.ToUInt32(chkActif.Checked)));
                sqlCommand.Parameters.AddWithValue("@production", Convert.ToString(Convert.ToUInt32(chkProduction.Checked)));
                sqlCommand.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)