第1个回答 2008-09-05
给你写个例子吧!
希望你能研究懂!
首先要对sqlclient引用;
程序头文件加上using System.Data.Sqlclient
程序中:
set conn=server.createobject("adodb.connection")
connstr="driver={SQL Server};server=local; uid=jsyc;pwd=123456;database=jzzs"
打开数据库:conn.open connstr
关闭呢是close
执行sql来完成增 删 查 改
string StrSQL = "INSERT INTO feed(id,mealie_buy,mealie_use,bean_buy,bean_use,medicine_buy,medicine_use,remark) VALUES('" + aa + "','" + bb + "','" + cc + "','" + dd + "','" + ee + "','" + ff + "','" + gg + "','" + h + "')";
SqlCommand cmd = new SqlCommand(StrSQL, myconn);
try
{
myconn.Open(); //打开数据库连接
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
da.Update(ds);
myconn.Close(); //关闭数据库连接
MessageBox.Show("增加成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception Err)
{
MessageBox.Show("增加操作失败:" + Err.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
第2个回答 2008-09-05
DataGridView用法示例2008-06-28 13:43private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridView view = (DataGridView)sender; //可将参数sender显示转化为DataGridView实例
label1.Text = "留言内容";
DataGridViewRow row = view.Rows[e.RowIndex]; //获取行,获取行中的具体的值,rows属性可获得DataGridViewRow中row 的一个集合,e.rowindex为点击的行号
//MessageBox.Show(row.Cells[0].Value.ToString() + row.Cells[1].Value.ToString() + row.Cells[2].Value.ToString() + row.Cells[3].Value.ToString());
this.dgv.Hide();
this.label1.Visible = true;
this.label2.Visible = true;
this.label3.Visible = true;
this.label4.Visible = true;
this.label5.Visible = true;
this.label6.Visible = true;
this.label7.Visible = true;
this.button1.Visible = true;
this.button2.Visible = false;
this.re.Visible = true;
this.textBox2.Visible = true;
this.textBox2.ReadOnly = true;
this.label2.Text = row.Cells[0].Value.ToString().Trim();
this.label3.Text = row.Cells[1].Value.ToString().Trim();
this.label4.Text = row.Cells[3].Value.ToString().Trim();
DataTable dtl = this.v3ta.GetDataBy(this.label2.Text.Trim(), this.label4.Text);//v3ta是一个TableAdapter的对象,GetDataBy是它的一个查询,返回一个datatable对象
if (dtl.Rows.Count != 0)
{
this.textBox2.Text = dtl.Rows[0].ItemArray[5].ToString().Trim() + "\r\n" + "----------------------------------------------------";//table对象dtl,也可以用rows,来获取一个ROW的集合,然后用索引访问每一个row
然后用row的ItemArray[index],访问row的每一个单元
int i;
for (i = 0; i < dtl.Rows.Count; i++)
this.textBox2.Text += "\r\n" + dtl.Rows[i].ItemArray[3].ToString().Trim() + "\t" + dtl.Rows[i].ItemArray[4].ToString().Trim() + "\t回复:\r\n\r\n" + dtl.Rows[i].ItemArray[2].ToString().Trim() + "\r\n";
}
else
this.textBox2.Text = row.Cells[2].Value.ToString();