DataAdapter操作出错至少一个参数没有被指定值。

游戏人间 2011-07-08 11:17:21
自已写了一个OleDbHelper类。调用时出错少一个参数没有被指定值。
代码如下:

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);
}
}



...全文
144 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
子夜__ 2011-07-10
  • 打赏
  • 举报
回复
检查是否缺少赋值 或者赋值失败。
yue547283947 2011-07-08
  • 打赏
  • 举报
回复
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";



写的很难看懂 但你肯定是某个字段没赋值


断点,首先看错误在哪个位置,在具体找问题。

你把这么多代码贴出来 看着能把人吓死!
windsea123 2011-07-08
  • 打赏
  • 举报
回复
列名不正确也会有这种提示
Ayanamikula 2011-07-08
  • 打赏
  • 举报
回复
你的图片太暴力了
孟子E章 2011-07-08
  • 打赏
  • 举报
回复
OleDbHelper.UpdateDateSet(updateCmd, insertCmd, deleteCmd, ds, tableName);
这行,你的parameters参数没有传递吧
游戏人间 2011-07-08
  • 打赏
  • 举报
回复
注册窗体仅测试 DataAdapter Update功能用。
游戏人间 2011-07-08
  • 打赏
  • 举报
回复
每个语句都放到Access库中都能被正确执行。

这名
da.Update(ds, tableName);
提示: 至少一个参数没有被指定值。

ly745455 2011-07-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 nicky1031 的回复:]
你的图片太暴力了
[/Quote]
+

62,073

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧