gpt4 book ai didi

c# - 使用 C# windows 窗体和 msaccess 添加记录

转载 作者:太空宇宙 更新时间:2023-11-03 17:04:24 24 4
gpt4 key购买 nike

我创建了一个 msaccess 数据库(MyWorkers.mdb),它包含一个表(tblWorkers)

代码是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication1
{
public partial class Insertfrm1 : Form
{
public OleDbConnection conn;
public OleDbDataAdapter da;
public DataSet ds;
public DataTable dt;
public OleDbCommand cmd;
public OleDbDataReader dr;
public int maxrecords;
public int pointer;
static public int cnt;
public string wid;

public Insertfrm1()
{
cnt++;
InitializeComponent();
}

private void Insertfrm1_Load(object sender, EventArgs e)
{
conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\MyWorkers1.mdb");
conn.Open();
pointer = 0;
filldata();
navigation();

}
public void filldata()
{
da = new OleDbDataAdapter("select * from tblWorkers", conn);
ds = new DataSet();
da.Fill(ds);
dt = ds.Tables[0];
maxrecords = dt.Rows.Count;
}
public void navigation()
{
if (dt.Rows.Count == 0)
{
MessageBox.Show("There is no Data available");
}
else
{
txtworkersid.Text = dt.Rows[pointer].ItemArray[0].ToString();
txtname.Text = dt.Rows[pointer].ItemArray[1].ToString();
txtjobtitle.Text = dt.Rows[pointer].ItemArray[2].ToString();
}
}

public void clrdata()
{
txtworkersid.Clear();
txtname.Clear();
txtjobtitle.Clear();
txtworkersid.Focus();
}

private void btnclear_Click(object sender, EventArgs e)
{
txtworkersid.Clear();
txtname.Clear();
txtjobtitle.Clear();
txtworkersid.Focus();
}

private void btnsave_Click(object sender, EventArgs e)
{
Boolean inc = false;
if (txtworkersid.Text == "")
{
DialogResult sav = MessageBox.Show("The Worker ID is Empty", "Blank Record", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (txtname.Text == "")
{
DialogResult sav = MessageBox.Show("The Workers Name is Empty", "Blank Record", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else if (txtjobtitle.Text == "")
{
DialogResult sav = MessageBox.Show("The Job Title is Empty", "Balnk record", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
//string cmdstr = "SELECT Workers_ID FROM tblWorkers WHERE (Workers_ID='" + txtworkersid.Text + "')";
//cmd = new OleDbCommand(cmdstr, conn);
cmd=new OleDbCommand("SELECT Workers_ID FROM tblWorkers WHERE (Workers_ID='" + txtname.Text + "')",conn);
cmd.ExecuteNonQuery();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (dr.Read())
{
if (txtworkersid.Text == dr.GetValue(0).ToString())
{
DialogResult sav = MessageBox.Show("The Record is Duplicate", "Duplicate Record", MessageBoxButtons.OK, MessageBoxIcon.Error);
inc = true;
break;
}
else
{
dr.NextResult();
}
}
if (inc == false)
{
string sql = "INSERT INTO tblWorkers(Workers_ID,name,job_title) VALUES ('" + txtworkersid.Text + "','" + txtname.Text + "','" + txtjobtitle.Text + "')";
Execute(sql);
DialogResult save = MessageBox.Show("Workers Record is saved Sucessfully.");
clrdata();
}
}
}

public void Execute(string sql)
{
//throw new NotImplementedException();
cmd = new OleDbCommand(sql, conn);
cmd.ExecuteNonQuery();
filldata();
}

private void txtworkersid_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
{
e.Handled = true;
DialogResult num = MessageBox.Show("Only Numbers are allowed", "invalid Key");
}
}

}
}

当我运行它时,它显示如下错误:条件表达式中的数据类型不匹配。

在 cmd.ExecuteNonQuery() 语句中请帮助我

谢谢

编辑:发布完整代码

最佳答案

您的问题在于您为要比较的 Workers_ID 传递的值。首先,您通过 txtname.Text,然后尝试将 txtworkersid.Text 插入该列。

应该是哪个?您应该将其保留为字符串,还是将其转换为数字(而不是用引号引起来)?

关于c# - 使用 C# windows 窗体和 msaccess 添加记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5880218/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com