这是一个Web表单应用程序,我在查找究竟是什么问题时遇到了一些麻烦。
我正在尝试将信息插入logins
表。
除了我添加[User]
时,一切都有效,这是当时登录用户的用户名。还有另一个用户名条目,但工作正常。
背后的Aspx.cs代码:
protected void add_Click(object sender, EventArgs e)
{
string currentUser = User.Identity.Name;
int webid = Convert.ToInt32(ddlWebsite.SelectedItem.Value);
string username = username.Text;
string _email = email.Text;
string pass = password.Text;
string Ainfo = info.Text;
string loguser = User.Identity.Name;
//string connectionstring = "Data Source=(LocalDb)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebProg-20170720092452.mdf;Initial Catalog=aspnet-WebProg-20170720092452;Integrated Security=true";
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString());
string query = "Insert into Logins(WebsiteID, Username, Password, AdditionalInfo, User, Email) Values ( @webid, @username, @pass, @Ainfo, @loguser, @_email)";
SqlCommand insertQuery = new SqlCommand(query,connection);
insertQuery.Parameters.AddWithValue("@webid", webid);
insertQuery.Parameters.AddWithValue("@username", username);
insertQuery.Parameters.AddWithValue("@pass", pass);
insertQuery.Parameters.AddWithValue("@Ainfo", Ainfo);
insertQuery.Parameters.AddWithValue("@loguser", User.Identity.Name);
insertQuery.Parameters.AddWithValue("@_email", _email);
connection.Open();
insertQuery.ExecuteNonQuery();
connection.Close();
}
ASPX标记:
添加登录
<div id="websiteDetails" class="col-md-4">
<label id="Label1" for="websiteName">
Website Name:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DefaultConnection %>" SelectCommand="SELECT [Id], [Name] FROM [WebsiteList]"></asp:SqlDataSource>
<asp:DropDownList ID="ddlWebsite" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="Id"></asp:DropDownList>
<asp:Button ID="Button1" runat="server" Text="AddAWebsite" />
</label>
<label id="Label3" for="username">
Username/Email:
<asp:TextBox ID="username" runat="server"></asp:TextBox>
</label>
<label id="Label4" for="password">
Password:
<asp:TextBox ID="password" runat="server"></asp:TextBox>
</label>
<label id="Label6" for="email">
Email Address:
<asp:TextBox ID="email" runat="server"></asp:TextBox>
</label>
<label id="Label5" for="info">
Additional Info:
<asp:TextBox ID="info" runat="server"></asp:TextBox>
</label>
</div>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"></asp:SqlDataSource>
<div id="loginDetails" class="col-md-4">
<p>Website not showing up in the list? Add a Website to the list.</p>
<label id="Label7" for="websiteName">
Website Name:
<asp:TextBox ID="Wname" runat="server"></asp:TextBox>
</label>
<label id="Label8" for="link">
Website Address:
<asp:TextBox ID="link" runat="server"></asp:TextBox>
</label>
</div>
<asp:Button ID="AddWebsite" runat="server" Text="Add Website" OnClick="AddWebsite_Click" />
</div>
<asp:Button ID="add" runat="server" Text="Add Login" class="btn btn-default" OnClick="add_Click" />
答案 0 :(得分:4)
好User
是一个保留字,这就是问题所在,可以在发布的INSERT
声明中看到。如果它是SQL Server,则需要像[User]
一样进行转义,或者使用双引号"User"
Insert into Logins(WebsiteID, Username, Password, AdditionalInfo, [User], Email)