我的应用程序sql语句中的逻辑错误

时间:2013-02-04 16:26:49

标签: asp.net

我的应用程序除了patientNumber之外,然后使用VisitNumbers弹出访问的下拉列表。每次访问都会为患者生成一个VisitNumber。 当我点击搜索按钮时,获取患者的visitNumber并填充我得到的Dropdownlist并错误:

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();
    }

1 个答案:

答案 0 :(得分: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的工作原理,就可以将其重新纳入代码