把SQL 连接到C#中显示如下错误:,我的代码是这样的:
 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
         string  str = "server=localhost;database=09130104;intergrated security=true;";
        
        private void Form1_Load(object sender, EventArgs e)
        {        }        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection(str);
                conn.Open();
                SqlCommand comm = new SqlCommand("select * from student where sno=textbox1.text", conn);
                DataTable dt = new DataTable();
                dt.Load(comm.ExecuteReader());
                dataGridView1.DataSource = dt;
               
            }
            catch (SqlException sm)
            {
                MessageBox.Show(sm.Message);
            }
            finally
            {
                conn.Close();
            }
            
        }
    }
}
求解答

解决方案 »

  1.   


    SqlCommand comm = new SqlCommand("select * from student where sno=textbox1.text", conn);

    改成
    string sno=textbox1.Text.Trim();
    string selectSQL = string.format("select * from student where sno={0}",sno);
    SqlCommand comm = new SqlCommand(selectSQL);

    试试,当然你要注意sno字段的数据类型,以及空值判断
      

  2.   


    SqlCommand comm = new SqlCommand("select * from student where sno=textbox1.text", conn);

    改成
    string sno=textbox1.Text.Trim();
    string selectSQL = string.format("select * from student where sno={0}",sno);
    SqlCommand comm = new SqlCommand(selectSQL);

    试试,当然你要注意sno字段的数据类型,以及空值判断
      

  3.   

    string str = "server=localhost;database=09130104;intergrated security=true;";
    看不到图估计是链接不上DB出错了微软提供了以下四种数据库连接方式:System.Data.OleDb.OleDbConnection
    System.Data.SqlClient.SqlConnection
    System.Data.Odbc.OdbcConnection
    System.Data.OracleClient.OracleConnection
    [/code下面我们以范例的方式,来依次说明:
    System.Data.SqlClient.SqlConnection
    [/code]
    常用的一些连接字符串(C#代码):SqlConnection conn 
    = new SqlConnection( "Server=(local);Integrated Security=SSPI;database=Pubs");SqlConnection conn 
    = new SqlConnection("server=(local)//NetSDK;database=pubs;Integrated Security=SSPI");SqlConnection conn = new SqlConnection(
    "Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");SqlConnection conn = new SqlConnection(
    " data source=(local);initial catalog=xr;integrated security=SSPI;
    persist security info=False;workstation id=XURUI;packet size=4096; ");SqlConnection myConn  = new 
    System.Data.SqlClient.SqlConnection("Persist Security Info=False;Integrated 
    Security=SSPI;database=northwind;server=mySQLServer");SqlConnection conn = new SqlConnection( 
    " uid=sa;pwd=passwords;initial catalog=pubs;data source=127.0.0.1;Connect Timeout=900");
      

  4.   

    SqlCommand comm = new SqlCommand("select * from student where sno=textbox1.text", conn); 错误是报在这句里么? 如果是这句的话。。"select * from student where sno="+textbox1.text+";", conn   但是你之前并没有声明和赋值给textbox1。。能不能取出我就不知道了