简单ACCESS处理问题

yuehuaqishi 2003-09-18 11:46:52
错误报告为:至少一个参数没有被指定值
//以下是源代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
namespace myApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtcomment;
private System.Windows.Forms.TextBox txtclass;
private System.Windows.Forms.TextBox txtname;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
//数据连接
private void GetConnect(string strSql)
{
string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+@"\mydb.mdb";
OleDbConnection myConn=new OleDbConnection(strConn);
myConn.Open();
OleDbCommand myCommand=new OleDbCommand(strSql,myConn);
myCommand.ExecuteNonQuery();
myConn.Close();
}
private void Run()
{
if(txtname.Text!="")
{
if(txtclass.Text!="")
{
string strSql="INSERT INTO base (proname,proclass,comment) VALUES ("+txtname.Text+","+txtclass.Text+","+txtcomment.Text+")";
GetConnect(strSql);
txtname.Clear();
txtclass.Clear();
txtcomment.Clear();
}
else
{
MessageBox.Show("请您输入产品阻值类别","错误");
}
}
else
{
MessageBox.Show("请您输入产品名字","错误");
}
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.txtcomment = new System.Windows.Forms.TextBox();
this.txtclass = new System.Windows.Forms.TextBox();
this.txtname = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button2,
this.button1,
this.txtcomment,
this.txtclass,
this.txtname,
this.label3,
this.label2,
this.label1});
this.groupBox1.Location = new System.Drawing.Point(0, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(320, 256);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
//
// button2
//
this.button2.Location = new System.Drawing.Point(232, 221);
this.button2.Name = "button2";
this.button2.TabIndex = 15;
this.button2.Text = "退出(&X)";
//
// button1
//
this.button1.Location = new System.Drawing.Point(119, 221);
this.button1.Name = "button1";
this.button1.TabIndex = 14;
this.button1.Text = "保存(&S)";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// txtcomment
//
this.txtcomment.Location = new System.Drawing.Point(103, 117);
this.txtcomment.Multiline = true;
this.txtcomment.Name = "txtcomment";
this.txtcomment.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal;
this.txtcomment.Size = new System.Drawing.Size(208, 80);
this.txtcomment.TabIndex = 13;
this.txtcomment.Text = "";
//
// txtclass
//
this.txtclass.Location = new System.Drawing.Point(103, 61);
this.txtclass.Name = "txtclass";
this.txtclass.Size = new System.Drawing.Size(208, 21);
this.txtclass.TabIndex = 12;
this.txtclass.Text = "";
//
// txtname
//
this.txtname.Location = new System.Drawing.Point(103, 13);
this.txtname.Name = "txtname";
this.txtname.Size = new System.Drawing.Size(208, 21);
this.txtname.TabIndex = 11;
this.txtname.Text = "";
//
// label3
//
this.label3.Location = new System.Drawing.Point(7, 125);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(64, 24);
this.label3.TabIndex = 10;
this.label3.Text = "说明备注";
//
// label2
//
this.label2.Location = new System.Drawing.Point(7, 69);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(64, 24);
this.label2.TabIndex = 9;
this.label2.Text = "阻值类别";
//
// label1
//
this.label1.Location = new System.Drawing.Point(7, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(64, 24);
this.label1.TabIndex = 8;
this.label1.Text = "产品名称";
//
// Form3
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(320, 263);
this.ControlBox = false;
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.groupBox1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MinimizeBox = false;
this.Name = "Form3";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "基础数据录入";
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{
Run();
}
}
}


谢谢各位了:
我在另外一个地方用:
string myStr="select username,userpass from user where username="+inputName.Text;
也出现了:至少有一个参数没有被指定值
我换成了
string myStr="select username,userpass from user";
就没有错误报告。
...全文
33 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
3no 2003-09-18
  • 打赏
  • 举报
回复
如username的字段类型是char型的话要这样写

string myStr="select username,userpass from user where username='"+inputName.Text+"'";

如果为int型的话,要先判断inputName.Text是否为空与是否为数字


这个东西一定要注意,你可以先在查询分析器里面把SQL语句调试好了在写进程序里
yuehuaqishi 2003-09-18
  • 打赏
  • 举报
回复
给分了
yuehuaqishi 2003-09-18
  • 打赏
  • 举报
回复
谢谢各位了
问题解决了
就是SQL语句的问题,而且按照大家的方法,一下子就解决了
wirte 2003-09-18
  • 打赏
  • 举报
回复
学习ing
cnhgj 2003-09-18
  • 打赏
  • 举报
回复
如username的字段类型是char型的话要这样写

string myStr="select username,userpass from user where username='"+inputName.Text+"'";

如果为int型的话,要先判断inputName.Text是否为空与是否为数字
yuehuaqishi 2003-09-18
  • 打赏
  • 举报
回复
谢谢各位大哥们:
我出现:至少一个参数没有被指定值
的地方两个
其一:
string strSql="INSERT INTO base (proname,proclass,comment) VALUES ("+txtname.Text+","+txtclass.Text+","+txtcomment.Text+")";
其二:
string myStr="select username,userpass from user where username="+inputName.Text;

现在我去试试按照你们说的修改测试一下

再次谢谢
jn_sly 2003-09-18
  • 打赏
  • 举报
回复
string strSql="INSERT INTO base (proname,proclass,comment) VALUES ("+txtname.Text+","+txtclass.Text+","+txtcomment.Text+")";
----->string strSql="INSERT INTO base (proname,proclass,comment) VALUES ('"+txtname.Text+"','"+txtclass.Text+"','"+txtcomment.Text+"')";
acewang 2003-09-18
  • 打赏
  • 举报
回复
sql语句有问题:
string myStr="select username,userpass from user where username="+inputName.Text;
------〉string myStr="select username,userpass from user where username='"+inputName.Text+"'";
csdn_bob 2003-09-18
  • 打赏
  • 举报
回复
最好的方式是用OleDbParameter类型来加入参数!这样可以减少很多的细节方面的错误,例如你的程序的!
timmy3310 2003-09-18
  • 打赏
  • 举报
回复
string myStr="select username,userpass from user where username="+inputName.Text;
改为:
string myStr="select username,userpass from user where username='"+inputName.Text+"'";
csdn_bob 2003-09-18
  • 打赏
  • 举报
回复
string myStr="select username,userpass from user where username="+inputName.Text;
如果inputName.Text为空呢?

string myStr=string.Format("select username,userpass from user where username='{0}'",inputName.Text);

wkyjob 2003-09-18
  • 打赏
  • 举报
回复
yuehuaqishi 2003-09-18
  • 打赏
  • 举报
回复
谢谢各位了
yuehuaqishi 2003-09-18
  • 打赏
  • 举报
回复
我把其中估计错误的地方单独列出吧,上面的代码太乱了,很多是无用的东西:
//数据连接
private void GetConnect(string strSql)
{
string strConn="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+@"\mydb.mdb";
OleDbConnection myConn=new OleDbConnection(strConn);
myConn.Open();
OleDbCommand myCommand=new OleDbCommand(strSql,myConn);
myCommand.ExecuteNonQuery();
myConn.Close();
}
private void Run()
{
if(txtname.Text!="")
{
if(txtclass.Text!="")
{
string strSql="INSERT INTO base (proname,proclass,comment) VALUES ("+txtname.Text+","+txtclass.Text+","+txtcomment.Text+")";
GetConnect(strSql);
txtname.Clear();
txtclass.Clear();
txtcomment.Clear();
}
else
{
MessageBox.Show("请您输入产品阻值类别","错误");
}
}
else
{
MessageBox.Show("请您输入产品名字","错误");
}
}

private void button1_Click(object sender, System.EventArgs e)
{
Run();
}

110,556

社区成员

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

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

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