同一个OleDbParameter对象不能重复OleDbCommand.Parameters.Add()

gunziyang 2014-05-12 09:29:30
sql语句有两个参数,需要赋同样的值,如下:select * from dept_dict where substr(dept_code, 0, ?) > '1000' and substr(dept_code, 0, ?) <> '1999'

如果OleDbParameter p = new OleDbParameter("prefixLength", OleDbType.Integer);
p.Value = 4;
OledbCommand.Parameters.Add(p);
OledbCommand.Parameters.Add(p);
则会报错“另一个 OleDbParameterCollection 中已包含 OleDbParameter”

又不想新建两个对象分别赋值,有什么办法吗?谢谢
...全文
115 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
gunziyang 2014-05-13
  • 打赏
  • 举报
回复
1楼正解,结贴了
gunziyang 2014-05-12
  • 打赏
  • 举报
回复
呼唤其他解法
宝_爸 2014-05-12
  • 打赏
  • 举报
回复
看看这里的第一个方法 搞定“另一个 OleDbParameterCollection 中已包含 OleDbParameter。”的两种办法。 http://www.cnblogs.com/ishare/archive/2009/04/28/1445341.html 第二个方法不适合你这里。
C#编程登陆界面代码 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; namespace 人事管理系统 { public partial class FormLogin : Form { public FormLogin() { InitializeComponent(); } private void FormLogin_Load(object sender, EventArgs e) { } public void buttonLogin_Click_1(object sender, EventArgs e) { try { if (textBoxName .Text =="") { MessageBox.Show ("用户名不能为空!","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning ); } else { if (textBoxPassword .Text =="") { MessageBox.Show("密码不能为空!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); } else { String connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\人事数据库.mdb"; OleDbConnection conn = new OleDbConnection(connStr); //String sql = "select count(*) from 用户密码表 where username='" + textBoxName.Text + "' and password='" + textBoxPassword.Text + "'"; conn.Open(); string sql = "select count(*) from 用户密码表 where username=@name and password=@pass"; OleDbCommand cmd = new OleDbCommand(sql ,conn ); cmd.Parameters.Add(new OleDbParameter ("@name",OleDbType .Char )); cmd.Parameters["@name"].Value = textBoxName.Text; cmd.Parameters.Add(new OleDbParameter("@pass", OleDbType.Char)); cmd.Parameters["@pass"].Value = textBoxPassword.Text; //OleDbDataAdapter da = new OleDbDataAdapter(sql, conn); //DataTable dt = new DataTable(); //da.Fill(dt); //int i = dt.Rows.Count; int i =(int )cmd.ExecuteScalar (); conn.Close(); if (i > 0) { FormMain fm = new FormMain(); fm.admintime = DateTime.Now.ToShortDateString(); fm.adminname = textBoxName.Text; fm.Show(); this.Hide (); } else { MessageBox.Show("用户名或密码错误!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error ); { textBoxName.Text = ""; textBoxPassword.Text = ""; textBoxName.Focus(); } } } } } catch (Exception ex) { MessageBox.Show(ex.ToString()); } } private void button1_Click(object sender, EventArgs e) { if (MessageBox.Show("您确定要退出本系统吗?", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK) { Application.Exit(); } } private void textBoxName_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { textBoxPassword.Focus(); e.Handled = true; } } private void textBoxPassword_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == 13) { buttonLogin.Focus(); e.Handled = true; } } private void linkLabeladd_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Form添加 fpty = new Form添加(); fpty.Show(); } private void linkLabelpass_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { Formpasswordmodify fpmd = new Formpasswordmodify(); fpmd.Show(); } } }

110,568

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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