private void Button1_Click(object sender, System.EventArgs e)
{ //连接SQL SERVER数据库 using System.Data.SqlClient;
SqlConnection cnn=new SqlConnection("server=(local);database=pubs;Integrated Security=SSPI");
SqlDataAdapter da=new SqlDataAdapter("select * from authors",cnn);
DataSet ds=new DataSet();
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
}
//******************************************************************************************
private void Button1_Click(object sender, System.EventArgs e)
{ //连接ACCESS数据库 "+Server.MapPath("
//using System.Data.OleDb;
String strConnection=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\db1.mdb";
OleDbConnection cn=new OleDbConnection(strConnection);
cn.Open();
OleDbDataAdapter da=new OleDbDataAdapter();
if (TextBox1.Text=="")
{
OleDbCommand adocmd= new OleDbCommand("select * from stu",cn);
da.SelectCommand=adocmd;
}
else
{
OleDbCommand adocmd= new OleDbCommand("select * from stu WHERE 姓名=@xm",cn);
adocmd.Parameters.Add("@xm",TextBox1.Text);
da.SelectCommand=adocmd;
}
DataSet ds=new DataSet();
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
cn.Close();
}
private void Button2_Click(object sender, System.EventArgs e)
{//增加
String strConnection=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\db1.mdb";
OleDbConnection cn=new OleDbConnection(strConnection);
cn.Open();
OleDbDataAdapter da=new OleDbDataAdapter();
OleDbCommand adocmd= new OleDbCommand("insert into stu(姓名,性别,年龄) VALUES (@xm,@xb,@nl)",cn);
adocmd.Parameters.Add("@xm",TextBox2.Text);//字段宽度超出回出错 需要限制
adocmd.Parameters.Add("@xb",TextBox3.Text);
adocmd.Parameters.Add("@nl",TextBox4.Text);
// da.SelectCommand=adocmd;
//da. cmdado.parameters.add("@valid",oledbtype.varchar);
//+TextBox1.Text
adocmd.ExecuteNonQuery();
cn.Close();
}
private void Button3_Click(object sender, System.EventArgs e)
{//修改
String strConnection=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\db1.mdb";
OleDbConnection cn=new OleDbConnection(strConnection);
cn.Open();
OleDbDataAdapter da=new OleDbDataAdapter();
OleDbCommand adocmd= new OleDbCommand("update stu set 年龄=@nl where (姓名=@xm)",cn);
adocmd.Parameters.Add("@nl",TextBox4.Text);//参数的顺序要与 前面的相同
adocmd.Parameters.Add("@xm",TextBox2.Text);
adocmd.ExecuteNonQuery();
cn.Close();
}
private void Button4_Click(object sender, System.EventArgs e)
{//删除
String strConnection=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\db1.mdb";
OleDbConnection cn=new OleDbConnection(strConnection);
cn.Open();
OleDbDataAdapter da=new OleDbDataAdapter();
OleDbCommand adocmd= new OleDbCommand("delete from stu where (姓名=@xm)",cn);
adocmd.Parameters.Add("@xm",TextBox2.Text);
adocmd.ExecuteNonQuery();
cn.Close();
}
*************************************************
//连接Excel 文件 using System.Data.OleDb;
String strConnection=@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\进销存表.xls;Extended Properties=Excel 8.0";
OleDbConnection cn=new OleDbConnection(strConnection);
cn.Open();
OleDbDataAdapter da=new OleDbDataAdapter();
if (TextBox1.Text=="")
{
OleDbCommand adocmd= new OleDbCommand("select * from [当前库存商品进销存表$]",cn);
da.SelectCommand=adocmd;
}
else
{
OleDbCommand adocmd= new OleDbCommand("select * from [当前库存商品进销存表$] WHERE 姓名=@xm",cn);
adocmd.Parameters.Add("@xm",TextBox1.Text);
da.SelectCommand=adocmd;
}
DataSet ds=new DataSet();
da.Fill(ds);
DataGrid1.DataSource=ds;
DataGrid1.DataBind();
cn.Close();
}