日期:2014-05-17 浏览次数:20759 次
private void button1_Click(object sender, EventArgs e)
{
//建立Sql查询语句
string sql = "select * from raifu";
//创建SQL命令行对象
SqlCommand cmd= new SqlCommand(sql,sqlCon);
//执行命令行对象
SqlDataReader odr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
IList<UserInfo> users = new List<UserInfo>();
while (odr.Read())
{
UserInfo user = new UserInfo();
user.UserID = Convert.ToInt32(odr["id"]);
user.UserName = Convert.ToString(odr["name"]);
user.UserAge = Convert.ToString(odr["age"]);
users.Add(user);
}
dataGridView1.Columns["col1"].DataPropertyName = "UserID";
dataGridView1.Columns["col2"].DataPropertyName = "UserName";
dataGridView1.Columns["col3"].DataPropertyName = "UserAge";
//绑定DataGridView控件
dataGridView1.DataSource = users;
//odr.Close();
}
//插入数据
private void button2_Click(object sender, EventArgs e)
{
//建立Sql语句
string sql = "insert into raifu (1003,'Jerry','30');";
//创建SQL命令行对象
SqlCommand cmd = new SqlCommand(sql, sqlCon);
//执行命令行对象 &