C# .net 求教asp.net从一个access表读出数据写入另一个表~~~~球代码

 我来答
ruheshi
2011-04-04 · TA获得超过157个赞
知道小有建树答主
回答量:623
采纳率:100%
帮助的人:422万
展开全部
我写的关于accsee的增删改查,希望对你有帮助
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Configuration;
using System.IO;//log需要

namespace AddressList
{
public partial class FrmAccessUse : Form
{
public FrmAccessUse()
{
InitializeComponent();
}
//access 不支持多条语句一起执行,所以不可以使用焦点值改变,生产sql语句的方法。
#region 调用access标准方法
// using System.Data.OleDb;
// using System.Data;
// 连接字符串:String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=product.mdb";
// 建立连接:OleDbConnection connection = new OleDbConnection(connectionString);
// 使用OleDbCommand类来执行Sql语句:
// OleDbCommand cmd = new OleDbCommand(sql, connection);
// connection.Open();
// cmd.ExecuteNonQuery();
#endregion

#region 连接字符串
//string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\access数据库操作\addressList.mdb"; //方法1,绝对路径
#region 全部保存2--优化的多行保存语句用的全局数组。
string strUpdate = "";//全局变量,修改保存语句
String[] szzhujian;
String[] szSQL;
#endregion
#endregion

#region 查询
private void tsbQuery_Click(object sender, EventArgs e)
{
query();
}
#endregion
#region 查询语句
private void query()
{
try
{
gridView1.Columns.Clear();
OleDbConnection connet1 = new OleDbConnection(strcon);
string sql = "select * from new1";
connet1.Open();
OleDbDataAdapter myadapter = new OleDbDataAdapter();
DataSet ds = new DataSet();
myadapter.SelectCommand = new OleDbCommand(sql, connet1);
myadapter.Fill(ds);
gridControl1.DataSource = ds.Tables[0];
connet1.Close();
gridControl1.Refresh();

#region 全部保存2--优化的多行保存语句
strUpdate = "";
szzhujian = null;
szSQL = null;
int j = this.gridView1.RowCount;
szzhujian = new string[j];
szSQL = new string[j];
#endregion
}
catch (Exception exp)
{

throw exp;
}
}
#endregion

#region 保存
private void tsbSave_Click(object sender, EventArgs e)
{
this.gridView1.FocusedRowHandle = -1; //关键语句,gridView1丢失焦点,进入值改变事件。
//Savebt();//单行保存
NewUpdate();//多行数据保存
}
#endregion
#region 保存语句
private void Savebt()
{
try
{
int[] rowCount = this.gridView1.GetSelectedRows();
string strSql = "";
foreach (int i in rowCount)
{
this.gridView1.FocusedRowHandle = -1;
string strID = "", strBH = "", strMC = "", strLXID = "", strLX = "", strSM = "", strFZ = "";
strID = this.gridView1.GetRowCellValue(i, "ZMID").ToString();
strBH = this.gridView1.GetRowCellValue(i, "帐目编号").ToString();
strMC = this.gridView1.GetRowCellValue(i, "帐目名称").ToString();
strLXID = this.gridView1.GetRowCellValue(i, "帐目类型ID").ToString();
strLX = this.gridView1.GetRowCellValue(i, "帐目类型").ToString();
strSM = this.gridView1.GetRowCellValue(i, "说明").ToString();
strFZ = this.gridView1.GetRowCellValue(i, "附注").ToString();
strSql = "Update new1 set 帐目编号='" + strBH + "', 帐目名称='" + strMC + "',帐目类型ID='" + strLXID + "',帐目类型='" + strLX + "',说明='" + strSM + "',附注='" + strFZ + "' where ZMID = " + strID + ""; //注意where处没有单引号
}
OleDbConnection connet1 = new OleDbConnection(strcon);
connet1.Open();
OleDbCommand cmd = new OleDbCommand(strSql, connet1);
int a = cmd.ExecuteNonQuery();
connet1.Close();
if (a > 0)
{
MessageBox.Show("保存成功!");
}
query();
}
catch (Exception exp)
{

throw exp;
}
}
#endregion
#region 优化的多行保存语句
private void NewUpdate()
{
try
{
if (szzhujian != null)
{
int a = 0;
OleDbConnection connet1 = new OleDbConnection(strcon);
connet1.Open();
for (int i = 0; i < szzhujian.Length; i++)
{

strUpdate = szSQL[i];
if (strUpdate != null)
{
OleDbCommand cmd = new OleDbCommand(strUpdate, connet1);
a = cmd.ExecuteNonQuery();
WriteMyLog(strUpdate);
}
}
connet1.Close();
if (a > 0)
{
MessageBox.Show("保存成功!");
}
query();
}
else
{
query();
}
}
catch (Exception exp)
{

throw exp;
}
}
#endregion

#region 添加一条新记录
private void tsbAddNew_Click(object sender, EventArgs e)
{
addnew();
}
#endregion
#region 添加语句
private void addnew()
{
try
{
string strSql = "";
OleDbConnection connet1 = new OleDbConnection(strcon);
connet1.Open();
strSql = "insert into new1(帐目编号) values('NewValues')";
OleDbCommand cmd = new OleDbCommand(strSql, connet1);
int a = cmd.ExecuteNonQuery();
connet1.Close();
if (a > 0)
{
query();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion

#region 删除
private void tsbDelete_Click(object sender, EventArgs e)
{
delete();
}
#endregion
#region 删除语句
private void delete()
{
try
{
int[] rowCount = this.gridView1.GetSelectedRows();
string strSql = "";
foreach (int i in rowCount)
{
this.gridView1.FocusedRowHandle = -1;
string strID = "";
strID = this.gridView1.GetRowCellValue(i, "ZMID").ToString();
strSql = "delete from new1 where ZMID=" + strID + ""; //注意where处没有单引号
}
OleDbConnection connet1 = new OleDbConnection(strcon);
connet1.Open();
OleDbCommand cmd = new OleDbCommand(strSql, connet1);
int a = cmd.ExecuteNonQuery();
connet1.Close();
if (a > 0)
{
MessageBox.Show("删除成功");
}
query();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);

}
}
#endregion

#region 列前序号
//事件gridView1_CustomDrawRowIndicator
private void gridView1_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
{
try
{
if (e.Info.IsRowIndicator)
{
if (e.RowHandle >= 0)
{
this.gridView1.IndicatorWidth = 40;
e.Info.DisplayText = Convert.ToString(e.RowHandle + 1);

}
}
}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}
#endregion
#region 事件当焦点行值改变后
private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
{
#region 全部保存--优化的多行保存语句(数组)

string strID = "", strSaveSql = "";
int[] rowCount = this.gridView1.GetSelectedRows();
foreach (int i in rowCount)
{
this.gridView1.FocusedRowHandle = -1;
string strBH = "", strMC = "", strLXID = "", strLX = "", strSM = "", strFZ = "";
strID = this.gridView1.GetRowCellValue(i, "ZMID").ToString();
strBH = this.gridView1.GetRowCellValue(i, "帐目编号").ToString();
strMC = this.gridView1.GetRowCellValue(i, "帐目名称").ToString();
strLXID = this.gridView1.GetRowCellValue(i, "帐目类型ID").ToString();
strLX = this.gridView1.GetRowCellValue(i, "帐目类型").ToString();
strSM = this.gridView1.GetRowCellValue(i, "说明").ToString();
strFZ = this.gridView1.GetRowCellValue(i, "附注").ToString();
strSaveSql = "Update new1 set 帐目编号='" + strBH + "', 帐目名称='" + strMC + "',帐目类型ID='" + strLXID + "',帐目类型='" + strLX + "',说明='" + strSM + "',附注='" + strFZ + "' where ZMID = " + strID + ""; //注意where处没有单引号
}

if (strID != "" && strSaveSql != "")
{
if (szzhujian[0] == null)
{
szzhujian[0] = strID;
szSQL[0] = strSaveSql;
}
else
{
for (int i = 0; i < szzhujian.Length; i++)
{
if (strID == szzhujian[i])
{
//return true;
szSQL[i] = strSaveSql;
break;
}
else
{
//return false;
if (i == szzhujian.Length - 1)
{
for (int k = 1; k < szzhujian.Length; k++)
{
if (szzhujian[k] == null)
{
szzhujian[k] = strID;
szSQL[k] = strSaveSql;
break;
}
}
}
//break;
}
}
}
}

#endregion
}
#endregion
#region 写log文件
public static void WriteMyLog(string ex)
{
string LOG_FOLDER = AppDomain.CurrentDomain.BaseDirectory + "Log";
try
{
//日志文件路径
string filePath = LOG_FOLDER + "\\" + DateTime.Now.ToShortDateString() + ".log";
if (!System.IO.Directory.Exists(LOG_FOLDER))//目录是否存在
{
Directory.CreateDirectory(LOG_FOLDER);
}
if (!File.Exists(filePath))//如果文件不存在
{
File.Create(filePath).Close();
}
StreamWriter sw = File.AppendText(filePath);
sw.WriteLine("-------------------------------------------------------------------------------------");
sw.WriteLine("Date:" + DateTime.Now.ToShortDateString() + " Time:" + DateTime.Now.ToShortTimeString());
sw.WriteLine(ex);
sw.Close();
}
catch (Exception exp)
{

throw exp;
}
}
#endregion

}
}
liaojielin
2011-04-05 · TA获得超过161个赞
知道答主
回答量:197
采纳率:0%
帮助的人:190万
展开全部
要查找的表检索出来,用GridView绑定,显示数据。
protected void Page_Load(object sender, EventArgs e)
{
this.Bind();
}
public void Bind()
{
SqlConnection con = DB.creatconnection();
SqlDataAdapter sda = new SqlDataAdapter("select * from table1", con);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
}
用一个按钮来写入另一张表table2。
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = DB.creatconnection();
con.Open();
SqlCommand cmd = new SqlCommand("insert into table2 (data)(select data from table1)",con);
cmd.ExecuteNonQuery();
Response.Write("插入成功。");
}
追问
("insert into table2 (data)(select data from table1)"具体里面怎么填呢?
异常详细信息: System.Data.OleDb.OleDbException: 至少一个参数没有被指定值。
源错误:
行 47: con.Open();
行 48: OleDbCommand cmd = new OleDbCommand("insert into cresume select * from uresume where uresume='"+Label1.Text+"' ", con);
行 49: cmd.ExecuteNonQuery();
追答
table1和table2分别两个字段,ID,data,ID为主键,且设置自动增长。
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式