-2

我的应用程序(除了患者编号)然后使用 VisitNumbers 填充访问的下拉列表。每次访问都会为患者生成一个访问编号。当我单击搜索按钮时,获取患者的 visitNumber 并填充我得到的下拉列表并出现错误:

Line 35:                 {
Line 36:                     con.Open();
Line 37:                     SqlDataReader reader = cmd.ExecuteReader();
Line 38:                     
Line 39:                     reader = cmd.ExecuteReader();



Stack Trace:


[SqlException (0x80131904): Incorrect syntax near '.'.
Incorrect syntax near 'a'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean  breakConnection, Action`1 wrapCloseInAction) +1753346

HERE IS MY CODE:

 public List<string> Get_VisitNumber()
    {
        List<string> visitnum = new List<string>();
        string patient_number = txtPatientNumber.Text;

        string connect =       
 System.Configuration.ConfigurationManager.ConnectionStrings["db_connection"].ToString();
        SqlConnection con = new SqlConnection(connect);
        string select_statement = "SELECT Visit_Number FROM Patient a ,Visit b"
          + "WHERE a.Patient_Number=b.Patient_Number";


        SqlCommand cmd = new SqlCommand(select_statement, con);


        cmd.Parameters.AddWithValue("@b.Patient_Number", patient_number);

        using (con)
        {
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                if (reader["Patient_Number"] != DBNull.Value)
                {
                    string VID = reader["Visit_Number"].ToString();
                    visitnum.Add(VID);
                }
                else
                {
                    Response.Write("<script language=javascript>alert('Patient Number not Found.');</script>");

                }


            }//   end while





        } // end using 

        return visitnum;
    }





    protected void btn_search_Click(object sender, EventArgs e)
    {
        DropDownList1.DataSource = Get_VisitNumber();

        DropDownList1.DataBind();
    }
4

1 回答 1

1

我将首先针对数据库测试您的 SQL,以确保您的语法正确,然后再尝试使其在代码中工作。

就像是

declare @patient_number int
set @patient_number = 1

SELECT b.Visit_Number 
FROM Patient a
join Visit b on (a.patient_number = b.patient_number)

WHERE a.Patient_Number = @Patient_Number

一旦你知道你的 SQL 可以工作,你就可以将它恢复到代码中

于 2013-02-04T17:36:14.763 回答