c#连接access数据库出错
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 test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string ID = this.mane.Text;
string k1 = this.ke.Text;
string k2 = this.key2.Text;
string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\test\CMS_DataManager.mdb";
if (k1 != k2)
{
MessageBox.Show("两次输入的密码不一致 \n 请重新输入!");
return;
}
string m = "select strUserName from SysBldUserDefine where strUserName=this.mane.Text";
string i = "insert into SysBldUserDefine (strUserName,strUserPassword) values ('this.mane.Text','this.ke.Text')";
OleDbConnection conn = new OleDbConnection(conStr);
OleDbCommand cmd = new OleDbCommand(m, conn);
try
{
conn.Open();
cmd.Parameters.Add(new OleDbParameter("mane", m));
int cnt = (int)cmd.ExecuteScalar();
if (cnt == 0)
{
cmd = new OleDbCommand(i, conn);
cmd.Parameters.Add(new OleDbParameter("strUserName", ID));
cmd.Parameters.Add(new OleDbParameter("strUserPassword", k1));
cmd.ExecuteNonQuery();
MessageBox.Show("注册成功!");
}
else
{
MessageBox.Show("该用户已存在 \n 请重新注册!");
}
}
catch(Exception ex)
{
MessageBox.Show("出现错误!");
}
finally
{
if (conn != null) conn.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
this.Hide();
f2.ShowDialog();
}
这个程序有什么问题吗 为什么运行就直接跳到 catch那里去了。我这程序主要是对数据库进行一个用户的添加 求救!!! --------------------编程问答-------------------- 把报错的堆栈信息打印出来,然后对症下药
catch(Exception ex)
{
MessageBox.Show(ex.message);
}
--------------------编程问答-------------------- 数据库连接字符串错了。
比如:
oleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "se.mdb;");
string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\test\CMS_DataManager.mdb";
=>
string conStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\test\CMS_DataManager.mdb";
--------------------编程问答-------------------- 连接字符串的,账户、密码呢? --------------------编程问答-------------------- catch(Exception ex)
弹出ex.Message --------------------编程问答-------------------- this.mane.Text 不应在引号之内 --------------------编程问答--------------------
这里不是这样直接写进去的where strUserName="+this.mane.Text+";这样 --------------------编程问答-------------------- string i = "insert into SysBldUserDefine (strUserName,strUserPassword) values ('this.mane.Text','this.ke.Text')";
应该改成
string i = "insert into SysBldUserDefine (strUserName,strUserPassword) values ('“+this.mane.Text+”','“+this.ke.Text+”')"; --------------------编程问答-------------------- where strUserName=this.mane.Text
改成
where strUserName=‘“+this.mane.Text+”’
补充:.NET技术 , C#