文件名称:datagridview增删改查
文件大小:1KB
文件格式:RAR
更新时间:2013-12-27 05:52:29
C# datagridview空间使用
private DataSet ds = new DataSet(); private SqlConnection conn = null; private SqlDataAdapter da = null; private const string DRIVER = "server=.;database=northwind;uid=sa;pwd=sa"; private const string sql_select = "select * from region"; /**//** * 此方法为将数据库northwind中的region表的数据查询出来并放入DataSet中 **/ private void Form1_Load(object sender, EventArgs e) ...{ conn = new SqlConnection(DRIVER); da = new SqlDataAdapter(sql_select,conn); da.Fill(ds,"table"); this.dataGridView1.DataSource = ds.Tables["table"].DefaultView; } private bool BtnInsert() //此方法作用于添加 ...{ da.InsertCommand = conn.CreateCommand(); da.InsertCommand.CommandText = "insert into region values(@id,@ption)"; da.InsertCommand.Parameters.Add("@id", SqlDbType.Int, 4, "regionid"); da.InsertCommand.Parameters.Add("@ption", SqlDbType.VarChar, 10, "regiondescription"); int count = da.Update(ds); bool result = count > 0 ? true : false; return result; } private void button1_Click(object sender, EventArgs e) ...{ if (this.BtnInsert())//调用此方法 ...{ MessageBox.Show("添加成功!"); } else ...{ MessageBox.Show("添加失败!"); } }
【文件预览】:
datagridview增删改查
----datagridview.txt(4KB)