日期:2014-05-17 浏览次数:20857 次
public void Form4_Load(object sender, EventArgs e) { sqlexe sqlexe=new sqlexe(); string sqlword = "select * from room"; sqlhelper sqldata = new sqlhelper();//实例化sqlhelper类,再调用类里的sqlconn 调用数据连接字符串 string strconn = sqldata.sqlconnstr; SqlConnection conn = new SqlConnection(strconn); //建立数据库连接 conn.Open(); //连接数据 SqlCommand cmd = new SqlCommand(); //创建sqlcommand对象 cmd.Connection = conn; //指定使用conn链接数据库 cmd.CommandText = sqlword; cmd.CommandType = CommandType.Text; SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd; DataSet ds = new DataSet(); sda.Fill(ds); dataGridView1.DataSource = ds.Tables[0]; conn.Close(); //return sda; //////////////////////////////////// } public void button1_Click(object sender, EventArgs e) { sqlexe sqlexe = new sqlexe(); string sqlword = "select * from room"; sqlhelper sqldata = new sqlhelper();//实例化sqlhelper类,再调用类里的sqlconn 调用数据连接字符串 string strconn = sqldata.sqlconnstr; SqlConnection conn = new SqlConnection(strconn); //建立数据库连接 conn.Open(); //连接数据 SqlCommand cmd = new SqlCommand(); //创建sqlcommand对象 cmd.Connection = conn; //指定使用conn链接数据库 cmd.CommandText = sqlword; cmd.CommandType = CommandType.Text; SqlDataAdapter sda = new SqlDataAdapter(); sda.SelectCommand = cmd; DataSet ds = new DataSet(); sda.Fill(ds); SqlCommandBuilder objBuilder = new SqlCommandBuilder(sda); sda.Update(ds); ds.AcceptChanges(); MessageBox.Show("success"); } public class sqlhelper //连接数据库文件sql语句 { public string sqlconnstr="Data Source=.\\SQLEXPRESS;User id=sa12;Pwd=12;database=wj"; //选择正确的数据库 }