C# SqlConnection连接sql server
try { SqlConnection conn = new SqlConnection(); conn.ConnectionString = "Data Source=127.0.0.1;Initial Catalog=123;User ID=sa;Password=123456"; conn.Open(); string sql = "SELECT * FROM table"; SqlDataAdapter myda = new SqlDataAdapter(sql, conn); DataTable dt = new DataTable(); myda.Fill(dt); dataGridView1.DataSource = dt; conn.Close(); } catch (Exception ex) { MessageBox.Show("错误消息:" + ex.Message, "出现错误"); }