注册 登录
编程论坛 ASP.NET技术论坛

求高手看看什么错误

zxgcoolboy 发布于 2012-06-24 12:35, 922 次点击

源代码如下:


String connString = @"Data Source=lusson-PC\SQLEXPRESS; AttachDbFilename=|DataDirectory|\alpine.mdf; Integrated Security=True; Connect Timeout=30; User Instance=True";
       String sql = @" SELECT * FROM cars WHERE " + Label1.Text;

       SqlConnection conn = new SqlConnection(connString);
       SqlDataAdapter adapter = new SqlDataAdapter(sql,conn);
       DataSet testDataSet = new DataSet();
       adapter.Fill(testDataSet, "result_data");
       conn.Close();
      
        foreach (DataRow testRow in testDataSet.Tables["result_data"].Rows)
            {
                   Console.WriteLine("id:{0} ; make:{1} ; model:{2}   ",
                    testRow["id"], testRow["make"], testRow["model"]
                    );
            }

编译没有错误,数据库连接成功,网页执行出错,错误信息如下:


Server Error in '/asp' Application.
Incorrect syntax near '='.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '='.

Source Error:

Line 35:        SqlDataAdapter adapter = new SqlDataAdapter(sql,conn);
Line 36:        DataSet testDataSet = new DataSet();
Line 37:        adapter.Fill(testDataSet, "result_data");
Line 38:        conn.Close();
Line 39:        


Source File: e:\stclaircollege\semester6\final project\asp\makes.aspx.cs    Line: 37


求高手给看看,语法哪里有错,说是在等于号附近, 找了半天没找出来,郁闷

先谢了
6 回复
#2
cnfarer2012-06-25 10:06
label1.text是什么?应该是SQL语句出了问题!
#3
zxgcoolboy2012-06-25 14:45
犯了个低级错误  label.text=" 1==0 OR make='honda'"

1==0   哈哈哈哈  ,版主就是版主  已经改过来了  label.text=" 1=0 OR make='honda'"

版主就是版主  佩服
#4
lcawen2012-06-26 17:29
数据库连接似乎木有打开吧?conn.Open();
 
#5
五当家2012-06-27 09:43
用SqlDataAdapter无需打开
#6
wmglh0012012-06-28 12:33
sql语句。。where条件后。。 有问题。
#7
absentlyh2012-06-29 09:05
label.text 这个值有问题. label.text的值是取不出来不能当做textbox用的吧
1