62,073
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
namespace DataSetTest
{
public class OleDbHelper
{
protected static string m_connectionstring = null;
public static string ConnectionString
{
get
{
if (m_connectionstring == null)
{
m_connectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Win\DataSetTest\App_Data\OA.mdb";
// m_connectionstring = ConfigurationManager.ConnectionStrings["LocalDb"].ConnectionString;
}
return m_connectionstring;
}
set
{
m_connectionstring = value;
}
}
public static DataSet GetDataSet(CommandType commandType, string commandText, params OleDbParameter[] commandParameters)
{
// 预处理
OleDbCommand cmd = new OleDbCommand();
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = ConnectionString;
connection.Open();
PrepareCommand(cmd, connection, commandType, commandText, commandParameters);
// 创建 DataAdapter & DataSet
using (OleDbDataAdapter da =new OleDbDataAdapter())
{
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
}
public static void UpdateDateSet(string updateCmd, string insertCmd, string deleteCmd, DataSet ds, string tableName)
{
//if (selectCmd == null) throw new ArgumentNullException("selectCmd");
if (updateCmd == null) throw new ArgumentNullException("updateCmd");
if (insertCmd == null) throw new ArgumentNullException("insertCmd");
if (deleteCmd == null) throw new ArgumentNullException("deleteCmd");
if (tableName == null || tableName.Length == 0) throw new ArgumentNullException("tableName");
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = ConnectionString;
connection.Open();
// 创建DbDataAdapter,当操作完成后释放.
using (OleDbDataAdapter da =new OleDbDataAdapter())
{
// 设置数据适配器命令
//dataAdapter.SelectCommand = new OleDbCommand(selectCmd, connection);
da.UpdateCommand = new OleDbCommand(updateCmd, connection);
da.InsertCommand = new OleDbCommand(insertCmd, connection);
da.DeleteCommand = new OleDbCommand(deleteCmd, connection);
// 更新数据集改变到数据库
da.Update(ds, tableName);
// 提交所有改变到数据集.
ds.AcceptChanges();
}
}
private static void PrepareCommand(OleDbCommand command, OleDbConnection connection, CommandType commandType, string commandText, OleDbParameter[] commandParameters)
{
if (command == null) throw new ArgumentNullException("command");
if (commandText == null || commandText.Length == 0) throw new ArgumentNullException("commandText");
// If the provided connection is not open, we will open it
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
// 给命令分配一个数据库连接.
command.Connection = connection;
// 设置命令文本(存储过程名或SQL语句)
command.CommandText = commandText;
// 设置命令类型.
command.CommandType = commandType;
// 分配命令参数
if (commandParameters != null)
{
command.Parameters.AddRange(commandParameters);
}
return;
}
}
}
public partial class UserRegister : Form
{
DataSet ds = null;
string tableName;
public UserRegister()
{
InitializeComponent();
}
private void UserRegister_Load(object sender, EventArgs e)
{
ds= OleDbHelper.GetDataSet(CommandType.Text, "SELECT UserId,UserName,Email,Remark FROM Users");
tableName=ds.Tables[0].TableName;
dataGridView1.DataSource = ds.Tables[0];
}
private void btnRegister_Click(object sender, EventArgs e)
{
// string selectCmd = "SELECT UserId,UserName,Email,Remark FROM Users";
string updateCmd="UPDATE Users SET UserName=@UserName,Email=@Email,Remark=@Remark WHERE UserId=@UserId";
string insertCmd = "INSERT INTO Users(UserId,UserName,Email,Remark) VALUES(@UserId,@UserName,@Email,@Remark)";
string deleteCmd = "DELETE FROM Users WHERE UserId=@UserId";
OleDbHelper.UpdateDateSet(updateCmd, insertCmd, deleteCmd, ds, tableName);
}
}
da.Update(ds, tableName);
提示: 至少一个参数没有被指定值。