不明白,, private void Form1_Load(object sender, System.EventArgs e) { string ConStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Sql_Sever\百里鲜\diaocha\fwdc\fwdc\bin\Debug\sjk.mdb"; try { // Get Data from multiple tables table string SQL = "SELECT * FROM 调查"; OleDbConnection Conn = new OleDbConnection(ConStr);
// Create a SqlCommand object oleDbCommand1 = new OleDbCommand(SQL, Conn); // Create a SqlDataAdapter object da = new OleDbDataAdapter(); da.SelectCommand = oleDbCommand1; // Create a SqlCommandBuilder object cb = new OleDbCommandBuilder(da); // Fill DataSet with data from Employees table da.Fill(dataSet1, "Employees"); // Bind the DataGrid at run time dataGrid1.SetDataBinding(dataSet1, "Employees"); } catch(Exception ex) { Console.WriteLine("err {0}",ex.Message); } } private void button1_Click(object sender, System.EventArgs e) {
{
string ConStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Sql_Sever\百里鲜\diaocha\fwdc\fwdc\bin\Debug\sjk.mdb";
try
{
// Get Data from multiple tables table
string SQL = "SELECT * FROM 调查"; OleDbConnection Conn = new OleDbConnection(ConStr);
// Create a SqlCommand object
oleDbCommand1 = new OleDbCommand(SQL, Conn); // Create a SqlDataAdapter object
da = new OleDbDataAdapter();
da.SelectCommand = oleDbCommand1; // Create a SqlCommandBuilder object
cb = new OleDbCommandBuilder(da); // Fill DataSet with data from Employees table
da.Fill(dataSet1, "Employees"); // Bind the DataGrid at run time
dataGrid1.SetDataBinding(dataSet1, "Employees");
}
catch(Exception ex)
{
Console.WriteLine("err {0}",ex.Message);
}
} private void button1_Click(object sender, System.EventArgs e)
{
da.Update(dataSet1, "Employees");
}修改完了,点button1就可以更新了
---------------------------------------
但我也有不明白的,
如果把连接 改称
string ConStr=@"Provider=SQLOLEDB; Data Source=(local); Initial Catalog=sjk;User ID=sa; Password=sa";更新时就会产生 UPDATE 无法使用SELECT语句。
----------------------
望高手指点
<%@ Page Language="C#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server"> private void BindGrid()
{
string strConn="Provider=Microsoft.Jet.OLEDB.4.0;"+"Data Source="+Server.MapPath("../chat/score.mdb");
OleDbConnection myConnection=new OleDbConnection();
myConnection.ConnectionString=strConn;
string cmd="Select * from score";
OleDbDataAdapter da=new OleDbDataAdapter(cmd,myConnection);
DataSet ds=new DataSet();
da.Fill(ds,"list");
DataGrid1.DataSource=ds.Tables["list"].DefaultView;
DataGrid1.DataBind();
}
private void Page_Load(Object Sender,EventArgs E)
{
if(!IsPostBack)
{
BindGrid();
}
}
private void DataGrid1_EditCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=(int)e.Item.ItemIndex;
BindGrid();
}
private void DataGrid1_CancelCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;
DataGrid1.DataBind();
}
private void DataGrid1_UpdateCommand(object source,System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string strConn="Provider=Microsoft.Jet.OLEDB.4.0;"+"Data Source="+Server.MapPath("../chat/score.mdb");
OleDbConnection myConnection=new OleDbConnection();
myConnection.ConnectionString=strConn;
string strUpdate="";
strUpdate+="stuID='"+((TextBox)e.Item.Cells[1].Controls[0]).Text+"'";
strUpdate+=",name='"+((TextBox)e.Item.Cells[2].Controls[0]).Text+"'";
strUpdate+=",chn="+((TextBox)e.Item.Cells[3].Controls[0]).Text;
strUpdate+=",math="+((TextBox)e.Item.Cells[4].Controls[0]).Text;
strUpdate+=",eng="+((TextBox)e.Item.Cells[5].Controls[0]).Text;
strUpdate+=",political="+((TextBox)e.Item.Cells[6].Controls[0]).Text;
string updateCmd="UPDATE score set "+strUpdate+" where stuID like '"+((TextBox)e.Item.Cells[1].Controls[0]).Text+"'";
OleDbCommand myCommand=new OleDbCommand(updateCmd,myConnection);
myCommand.Connection.Open();
try
{
myCommand.ExecuteNonQuery();
DataGrid1.EditItemIndex=-1;
}
catch
{
Response.Write("<script Language='Javascript'> alert('未能更新记录,请确保正确填写了字段!')<" + "/script>");
}
BindGrid();
}
void DataGrid1_SelectedIndexChanged(object sender, EventArgs e) {
}</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:Label id="Label1" runat="server">更新数据实例</asp:Label>
</p>
<p>
<asp:DataGrid id="DataGrid1" runat="server" OnSelectedIndexChanged="DataGrid1_SelectedIndexChanged">
<Columns>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="更新" HeaderText="EditCommand" CancelText="取消" EditText="编辑"></asp:EditCommandColumn>
</Columns>
</asp:DataGrid>
</p>
<!-- Insert content here -->
</form>
</body>
</html
我用的就是Access
然后分别在编辑EditCommand、更新UpdateCommand、取消CancelCommand中写上相应的代码