新建数据库连接:
conn = new SqlConnection();
conn.ConnectionString = "Data Source=.//SQLEXPRESS;AttachDbFilename=|DataDirectory|//SQLDemo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
conn.Open();
conn.Close();
conn = new SqlConnection();
conn.ConnectionString = "Data Source=.//SQLEXPRESS;AttachDbFilename=|DataDirectory|//SQLDemo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
conn.Open();
conn.Close();
用sqlDataadapter读取数据库:
conn = new SqlConnection();
conn.ConnectionString = "Data Source=.//SQLEXPRESS;AttachDbFilename=|DataDirectory|//SQLDemo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
cmd = new SqlCommand("select * from student",conn);
adpter = new SqlDataAdapter(cmd);
adpter = new SqlDataAdapter(cmd);
bulit = new SqlCommandBuilder(adpter);
//可以使用sqldataadpter更新数据库 this.adpter.Update(this.ds, "locStd");
//可以使用sqldataadpter更新数据库 this.adpter.Update(this.ds, "locStd");
ds = new DataSet();
adpter.Fill(ds, "locStd");
adpter.Fill(ds, "locStd");
this.dataGridView1.DataSource = ds.Tables["locStd"];
用sqlDataReader读取数据库:
conn = new SqlConnection();
conn.ConnectionString = "Data Source=.//SQLEXPRESS;AttachDbFilename=|DataDirectory|//SQLDemo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
conn.Open();
conn.ConnectionString = "Data Source=.//SQLEXPRESS;AttachDbFilename=|DataDirectory|//SQLDemo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "select * from student";
SqlDataReader reader = cmd.ExecuteReader();
cmd.Connection = conn;
cmd.CommandText = "select * from student";
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
this.listBox1.Items.Add(reader[0].ToString() + "," + reader["name"].ToString() + " ," + reader["age"].ToString()
+ "," + reader["city"].ToString() );
}
reader.Close();
conn.Close();
{
this.listBox1.Items.Add(reader[0].ToString() + "," + reader["name"].ToString() + " ," + reader["age"].ToString()
+ "," + reader["city"].ToString() );
}
reader.Close();
conn.Close();
使用sqlcommand更新数据库:
conn = new SqlConnection();
conn.ConnectionString = "Data Source=.//SQLEXPRESS;AttachDbFilename=|DataDirectory|//SQLDemo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
conn.Open();
conn = new SqlConnection();
conn.ConnectionString = "Data Source=.//SQLEXPRESS;AttachDbFilename=|DataDirectory|//SQLDemo.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
conn.Open();
cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "Update student set city = 'beijing'where id = @id" ;
SqlParameter id = new SqlParameter(id,SqlDbType.Int);
id.Value = Convert.ToInt32(this.textBox1.Text);
cmd.Connection = conn;
cmd.CommandText = "Update student set city = 'beijing'where id = @id" ;
SqlParameter id = new SqlParameter(id,SqlDbType.Int);
id.Value = Convert.ToInt32(this.textBox1.Text);
cmd.ExecuteNonQuery();
conn.Close();
conn.Close();