1、點擊視圖,點擊服務器資源管理
2、點擊鏈接到數據庫
3、在出來的界面中點擊數據源,點擊下圖所示
4、選擇數據庫名等
服務器名在SQL看
這個字符串就是需要建立實例的參數
5、在click事件中可以這樣寫代碼
SqlConnection comn = new SqlConnection("Data Source = JINQI; Initial Catalog = Test; Integrated Security = True"); try { string search = "select * from matnr where(0=0)"; if (this.textBox1.Text.Length>0) { search = search + " and 物料編碼=" + "'" + textBox1.Text + "'"; } if (this.textBox2.Text.Length > 0) { search = search + " and 物料描述=" + "'" + textBox2.Text + "'"; } SqlDataAdapter sda = new SqlDataAdapter(search, comn); DataSet ds = new DataSet(); sda.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; dataGridView1.RowHeadersVisible = false; } catch (Exception EX) { MessageBox.Show(EX.Message+EX.StackTrace); }