在asp.net中使用sqlconnection的问题c#

Ish*_*han 0 c# asp.net sql-server-2000

我的代码:

String dbDate = DateTime.ParseExact(TextBox3.Text, "dd/mm/yyyy", null).ToString("yyyy-mm-dd");

SqlConnection MyConnection = new SqlConnection("Data Source=localhost;Initial Catalog=hcgoa;User Id=sa;Password=;");
MyConnection.Open();
String MyString = "select notice from notice_aspx where fil_no=? and orderdate=?";
SqlCommand MyCmd = new SqlCommand(MyString, MyConnection);
MyCmd.Parameters.AddWithValue("?", HiddenField4.Value);
MyCmd.Parameters.AddWithValue("?", dbDate);
using (SqlDataReader MyReader4 = MyCmd.ExecuteReader())
{
    //**
    if (MyReader4.Read())
    {

        String MyString1 = "UPDATE notice_aspx SET notice=? where fil_no=? AND orderdate=?";
        SqlCommand MyCmd1 = new SqlCommand(MyString1, MyConnection);
        MyCmd1.Parameters.AddWithValue("?", Editor1.Content.ToString());
        MyCmd1.Parameters.AddWithValue("?", HiddenField4.Value.ToString());
        MyCmd1.Parameters.AddWithValue("?", dbDate);
        MyCmd1.ExecuteNonQuery();
    }
    else
    {.........
Run Code Online (Sandbox Code Playgroud)

错误是

Exception Details: System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near '?'.
Line 1: Incorrect syntax near '?'.
Run Code Online (Sandbox Code Playgroud)

如何纠正错误.是因为我不能用'?' ?请帮助解决问题.

Jon*_*eet 5

我相信对于SQL服务器,您需要使用命名参数而不是位置参数.有关SqlCommand.Parameters示例,请参阅文档.所以你的SQL将是:

select notice from notice_aspx where fil_no=@fil and orderdate=@orderdate
Run Code Online (Sandbox Code Playgroud)

(然后在添加参数时指定这些名称).

请注意,这与ASP.NET无关 - 您应该能够在一个小型控制台应用程序中进行检查.(你也应该有using你的连接和命令的声明,顺便说一句.)