C# 为什么我';我在';附近得到了错误的语法)';错误?
我正在尝试使用Visual Basic 2012上的C#创建注册页面。当我调试时,我得到0个错误,但当我尝试注册一个帐户时,我得到以下错误 “靠近“)”的语法不正确” 如果我尝试使用现有用户名创建一个帐户,则表明该用户名已存在。所以我可以连接到SQL server,但我不确定哪里出了问题 此注册页面应在my DB DNMembership>Table>accounts中创建帐户 这是我正在使用的代码C# 为什么我';我在';附近得到了错误的语法)';错误?,c#,asp.net,sql,sql-server,syntax,C#,Asp.net,Sql,Sql Server,Syntax,我正在尝试使用Visual Basic 2012上的C#创建注册页面。当我调试时,我得到0个错误,但当我尝试注册一个帐户时,我得到以下错误 “靠近“)”的语法不正确” 如果我尝试使用现有用户名创建一个帐户,则表明该用户名已存在。所以我可以连接到SQL server,但我不确定哪里出了问题 此注册页面应在my DB DNMembership>Table>accounts中创建帐户 这是我正在使用的代码 { SqlConnection con = new SqlConnection(Conf
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegDNMembershipConnectionString"].ConnectionString);
con.Open();
string insCmd = "Insert into Accounts (AccountName, Passphrase, EmailAddress, FullName, Country)";
SqlCommand insertUser = new SqlCommand(insCmd, con);
insertUser.Parameters.AddWithValue("@AccountName", TextBoxUN.Text);
insertUser.Parameters.AddWithValue("@Passphrase", TextBoxPass.Text);
insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
insertUser.Parameters.AddWithValue("@FullName", TextBoxFN.Text);
insertUser.Parameters.AddWithValue("@Country", DropDownListCountry.SelectedItem.ToString());
try
{
insertUser.ExecuteNonQuery();
con.Close();
Response.Redirect("Login.aspx");
}
catch(Exception er)
{
Response.Write("<b>Something Really Bad Happened... Please Try Again.< /br></b>");
Response.Write(er.Message);
}
{
SqlConnection con=新的SqlConnection(ConfigurationManager.ConnectionString[“RegDNMembershipConnectionString”].ConnectionString);
con.Open();
string insCmd=“插入帐户(帐户名、密码短语、电子邮件地址、全名、国家)”;
SqlCommand insertUser=新的SqlCommand(insCmd,con);
insertUser.Parameters.AddWithValue(“@AccountName”,TextBoxUN.Text);
insertUser.Parameters.AddWithValue(“@Passphrase”,TextBoxPass.Text);
insertUser.Parameters.AddWithValue(“@EmailAddress”,TextBoxEA.Text);
insertUser.Parameters.AddWithValue(“@FullName”,TextBoxFN.Text);
insertUser.Parameters.AddWithValue(“@Country”,DropDownListCountry.SelectedItem.ToString());
尝试
{
insertUser.ExecuteOnQuery();
con.Close();
重定向(“Login.aspx”);
}
捕获(异常er)
{
回答。写下(“发生了非常糟糕的事情……请再试一次。”;
响应.写入(er.消息);
}
我做错了什么?看起来您忘了在命令中添加
值
值
介绍要插入的一个或多个数据值列表。必须有
列_列表中的每一列都有一个数据值(如果指定),或
表。值列表必须用括号括起来
更改您的sql查询,如
string insCmd = @"Insert into Accounts (AccountName, Passphrase, EmailAddress, FullName, Country)
VALUES(@AccountName, @Passphrase, @EmailAddress, @FullName, @Country)";
并用于处理您的SqlConnection
和SqlCommand
等
using(SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegDNMembershipConnectionString"].ConnectionString))
{
using(SqlCommand insertUser = new...)
{
//Your code..
}
}
您尚未在SQL中指定任何参数,或在value
部分中指定任何参数-您说的是“我想插入这些字段…”,但不是您想插入的内容。它应该类似于:
string insCmd =
"Insert into Accounts (AccountName, Passphrase, EmailAddress, FullName, Country) "
+ "Values (@AccountName, @Passphrase, @EmailAddress, @FullName, @Country");
您需要更改SQL语句:
string insCmd = "Insert into Accounts (AccountName, Passphrase, EmailAddress, FullName, Country) VALUES (@AccountName,@Passphrase,@EmailAddress,@FullName,@Country)";
您缺少Insert
语句的一部分
INSERT INTO table (col1, col2) VALUES (@col1, @col2)
或者,若要按列在表中的顺序将所有值插入列中
INSERT INTO table VALUES (@col1, @col2)
SQL Server中的INSERT
命令有多种选择
- 指定
列
,然后指定值
- SQL语法-
插入到表中(帐户名、密码、电子邮件地址、全名、国家)
值('AccountName','Passphrase','EmailAddress','FullName','Country')
- C#
string insCmd=“插入到表中(帐户名、密码、电子邮件地址、全名、国家/地区)
值(@AccountName、@Passphrase、@EmailAddress、@FullName、@Country)”
- 如果您确定可以跳过指定列的列顺序,那么如果您将
值的顺序弄错,将有可能在错误的列中插入值
- SQL Sytanx-
插入表值('AccountName','Passphrase','EmailAddress','FullName','Country')
- C#
string insCmd=“插入表值(@AccountName、@Passphrase、@EmailAddress、@FullName、@Country)”
好的阅读资源是
- W3学校-
- Technet-
除了插入到表中的之外
您可以从插入到表中的C#
调用存储过程。使用存储过程可以帮助您减少临时查询,帮助防止SQL注入,减少网络流量,添加额外的验证服务器端。您的代码如下所示
SqlCommand cmd = new SqlCommand("usp_InsertIntoAccount", con);
con.Open();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@AccountName", TextBoxUN.Text));
cmd.Parameters.Add(new SqlParameter("@Passphrase", TextBoxPass.Text));
cmd.Parameters.Add(new SqlParameter("@EmailAddress", TextBoxEA.Text));
cmd.Parameters.Add(new SqlParameter("@FullName", TextBoxFN.Text));
cmd.Parameters.Add(new SqlParameter("@Country", DropDownListCountry.SelectedItem.ToString()));
try
{
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("Login.aspx");
}
catch(Exception er)
{
Response.Write("<b>Something Really Bad Happened... Please Try Again.< /br></b>");
Response.Write(er.Message);
}
SqlCommand cmd=newsqlcommand(“usp_insertintoccount”,con);
con.Open();
cmd.CommandType=CommandType.storedProcess;
Add(新的SqlParameter(“@AccountName”,TextBoxUN.Text));
Add(新的SqlParameter(“@Passphrase”,TextBoxPass.Text));
Add(新的SqlParameter(“@EmailAddress”,TextBoxEA.Text));
Add(新的SqlParameter(“@FullName”,TextBoxFN.Text));
cmd.Parameters.Add(新的SqlParameter(“@Country”,DropDownListCountry.SelectedItem.ToString());
尝试
{
cmd.ExecuteNonQuery();
con.Close();
重定向(“Login.aspx”);
}
捕获(异常er)
{
回答。写下(“发生了非常糟糕的事情……请再试一次。”;
响应.写入(er.消息);
}
以下问题的答案中列出了其他资源您需要掌握SQL。它仅在运行时执行。字符串中的SQL语法不会出现任何编译器错误。