C# 插入sql表时出现问题
我有一个我正在开发的网站,当用户创建一个帐户时,我想存储我在CreateUserWizard期间收集的一些附加信息。当我运行代码时,我在尝试将数据写入我创建的表时出错。我在ASPNETDB.mdf数据库(默认情况下创建的数据库)中创建了名为UserProfile的表,其中包含我试图写入的所有列。在web.config文件中,我有:C# 插入sql表时出现问题,c#,sql,C#,Sql,我有一个我正在开发的网站,当用户创建一个帐户时,我想存储我在CreateUserWizard期间收集的一些附加信息。当我运行代码时,我在尝试将数据写入我创建的表时出错。我在ASPNETDB.mdf数据库(默认情况下创建的数据库)中创建了名为UserProfile的表,其中包含我试图写入的所有列。在web.config文件中,我有: <connectionStrings> <add name="ApplicationServices" connectionString="da
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
我在myComman.ExecuteNonQuery()上得到一个错误;声明--必须声明标量变量“@UserRole”。如果我注释掉代码,它将不会向数据库表写入任何内容。我做错了什么?您有
myCommand.Parameters.AddWithValue(“@Role”,UserRole)
,但我认为您应该有myCommand.Parameters.AddWithValue(“@UserRole”,UserRole)代码> 您有myCommand.Parameters.AddWithValue(“@Role”,UserRole)
,但我认为您应该有myCommand.Parameters.AddWithValue(“@UserRole”,UserRole)代码> 对于任何关心此事的人……以下是工作代码:
protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e)
{
string trimUserName = RegisterUser.UserName.Trim();
if (RegisterUser.UserName.Length != trimUserName.Length)
{
accountlbl.Text = "The username cannot contain leading or trailing spaces.";
accountlbl.Visible = true;
//Cancel the created user info
e.Cancel = true;
}
if (RegisterUser.Password.IndexOf(RegisterUser.UserName, StringComparison.OrdinalIgnoreCase) > 0)
{
accountlbl.Text = "The username may not appear anywhere in the password.";
accountlbl.Visible = true;
//Cancel the created user info
e.Cancel = true;
}
String FirstName = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;
String LastName = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;
String Company = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("CompanyName")).Text;
String PartsList = ((DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("companyddl")).SelectedValue;
String UserName = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).Text;
String Password = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Password")).Text;
String Email = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Email")).Text;
String Question = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Question")).Text;
String Answer = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Answer")).Text;
// Insert a new record into User_Profile
if (((Company == "OurCompany" || Company == "OurCompany, Inc.") && PartsList == ""))
{
if (!User.IsInRole("manager"))
{
Roles.AddUserToRole(UserName, "manager");
Role = "manager";
}
}
if (PartsList == "SF")
{
if (!User.IsInRole("user_sf"))
{
Roles.AddUserToRole(UserName, "user_sf");
Role = "user_sf";
}
}
if (PartsList == "TL")
{
if (!User.IsInRole("user_tl"))
{
Roles.AddUserToRole(UserName, "user_tl");
Role = "user_tl";
}
}
if ((Company != "OurCompany" || Company != "OurCompany, Inc.") && PartsList == "")
{
if (!User.IsInRole("user"))
{
Roles.AddUserToRole(UserName, "user");
Role = "user";
}
}
////Get your Connection String from the web.config...ApplicationServices is the name I have in my web.config
string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
string insertSql = "INSERT INTO UserProfile(FirstName, LastName, Company, PartsList, UserName, Password, Email, Question, Answer, Role)" +
"VALUES(@FirstName, @LastName, @Company, @Partslist, @UserName, @Password, @Email, @Question, @Answer, @Role)";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
myCommand.Parameters.AddWithValue("@FirstName", FirstName);
myCommand.Parameters.AddWithValue("@LastName", LastName);
myCommand.Parameters.AddWithValue("@Company", Company);
myCommand.Parameters.AddWithValue("@PartsList", PartsList);
myCommand.Parameters.AddWithValue("@UserName", UserName);
myCommand.Parameters.AddWithValue("@Password", Password);
myCommand.Parameters.AddWithValue("@Email", Email);
myCommand.Parameters.AddWithValue("@Question", Question);
myCommand.Parameters.AddWithValue("@Answer", Answer);
myCommand.Parameters.AddWithValue("@Role", Role);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
对于任何关心此事的人……以下是工作代码:
protected void RegisterUser_CreatingUser(object sender, LoginCancelEventArgs e)
{
string trimUserName = RegisterUser.UserName.Trim();
if (RegisterUser.UserName.Length != trimUserName.Length)
{
accountlbl.Text = "The username cannot contain leading or trailing spaces.";
accountlbl.Visible = true;
//Cancel the created user info
e.Cancel = true;
}
if (RegisterUser.Password.IndexOf(RegisterUser.UserName, StringComparison.OrdinalIgnoreCase) > 0)
{
accountlbl.Text = "The username may not appear anywhere in the password.";
accountlbl.Visible = true;
//Cancel the created user info
e.Cancel = true;
}
String FirstName = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("FirstName")).Text;
String LastName = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("LastName")).Text;
String Company = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("CompanyName")).Text;
String PartsList = ((DropDownList)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("companyddl")).SelectedValue;
String UserName = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("UserName")).Text;
String Password = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Password")).Text;
String Email = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Email")).Text;
String Question = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Question")).Text;
String Answer = ((TextBox)RegisterUser.CreateUserStep.ContentTemplateContainer.FindControl("Answer")).Text;
// Insert a new record into User_Profile
if (((Company == "OurCompany" || Company == "OurCompany, Inc.") && PartsList == ""))
{
if (!User.IsInRole("manager"))
{
Roles.AddUserToRole(UserName, "manager");
Role = "manager";
}
}
if (PartsList == "SF")
{
if (!User.IsInRole("user_sf"))
{
Roles.AddUserToRole(UserName, "user_sf");
Role = "user_sf";
}
}
if (PartsList == "TL")
{
if (!User.IsInRole("user_tl"))
{
Roles.AddUserToRole(UserName, "user_tl");
Role = "user_tl";
}
}
if ((Company != "OurCompany" || Company != "OurCompany, Inc.") && PartsList == "")
{
if (!User.IsInRole("user"))
{
Roles.AddUserToRole(UserName, "user");
Role = "user";
}
}
////Get your Connection String from the web.config...ApplicationServices is the name I have in my web.config
string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
string insertSql = "INSERT INTO UserProfile(FirstName, LastName, Company, PartsList, UserName, Password, Email, Question, Answer, Role)" +
"VALUES(@FirstName, @LastName, @Company, @Partslist, @UserName, @Password, @Email, @Question, @Answer, @Role)";
using (SqlConnection myConnection = new SqlConnection(connectionString))
{
myConnection.Open();
SqlCommand myCommand = new SqlCommand(insertSql, myConnection);
myCommand.Parameters.AddWithValue("@FirstName", FirstName);
myCommand.Parameters.AddWithValue("@LastName", LastName);
myCommand.Parameters.AddWithValue("@Company", Company);
myCommand.Parameters.AddWithValue("@PartsList", PartsList);
myCommand.Parameters.AddWithValue("@UserName", UserName);
myCommand.Parameters.AddWithValue("@Password", Password);
myCommand.Parameters.AddWithValue("@Email", Email);
myCommand.Parameters.AddWithValue("@Question", Question);
myCommand.Parameters.AddWithValue("@Answer", Answer);
myCommand.Parameters.AddWithValue("@Role", Role);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
我喜欢做一些你看不到明显的事情…谢谢!现在我得到一个错误——参数化查询“(@FirstName-nvarchar(6),@LastName-nvarchar(9),@Company-nvarchar”(“需要参数“@UserRole”),但没有提供。我是否需要添加类似以下内容:myCommand.Parameters.AddWithValue(@FirstName”,SqlDbType.nvarchar,FirstName);其中SqlDbType与数据库中的实际数据类型匹配?是否UserRole
包含值或为null?如果允许null值,则将DBNull.value
作为参数值放入。UserRole应在代码中之前为If赋值。在数据库表中,不允许null值。是否确定参数ter设置是否正确?您应该在执行命令之前调试它和/或转储所有参数。我认为在某些情况下,UserRole
可以包含空值。我刚才注意到的一件事是,在我的表中,最后一列是“Role”,我试图分配一个“UserRole”,但是我还是收到了一个错误消息。在执行命令之前,我如何检查参数并将其全部转储?我一定很喜欢做一些你看不到明显的东西…谢谢!现在我收到了一个错误-参数化查询“(@FirstName nvarchar(6),@LastName nvarchar(9),@Company nvarchar('需要未提供的参数'@UserRole'。是否需要添加类似以下内容:myCommand.Parameters.AddWithValue(“@FirstName”,SqlDbType.NVarChar,FirstName);其中SqlDbType与数据库中的实际数据类型匹配?是否UserRole
包含值或为null?如果允许null值,则将DBNull.value
作为参数值放入。UserRole应在代码中之前为If赋值。在数据库表中,不允许null值。是否确定参数ter设置是否正确?您应该在执行命令之前调试它和/或转储所有参数。我认为在某些情况下,UserRole
可以包含空值。我刚才注意到的一件事是,在我的表中,最后一列是“Role”,我试图分配一个“UserRole”,但我仍然收到一条错误消息。如何在执行命令之前检查参数并将其全部转储?