gpt4 book ai didi

c# - 网络表单中的这个插入查询抛出错误,我不确定为什么

转载 作者:太空宇宙 更新时间:2023-11-03 21:01:16 27 4
gpt4 key购买 nike

这是一个 Web 表单应用程序,我很难找出它究竟出了什么问题。

我正在尝试将信息插入到 logins 表中。

当我添加 [User] 时,一切正常,这是当时登录人员的用户名。用户名还有另一个条目,但工作正常。

Image Of The Logins Table

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" />

This is the error message that doesn't tell me much at all

最佳答案

User 是一个保留字,这就是这里的问题,从您发布的 INSERT 声明中可以看出。如果它是 SQL Server,则需要像 [User] 一样进行转义,或者使用双引号 "User"

进行 ANSI 样式转义
Insert into Logins(WebsiteID, Username, Password, AdditionalInfo, [User], Email)

关于c# - 网络表单中的这个插入查询抛出错误,我不确定为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45301099/

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