调试代码时出现错误(调试之前不显示任何错误)以下是我在Visual Studio 2017中使用C#编写的代码
using MySql.Data;
using MySql.Data.MySqlClient;
namespace MARUBI
{
public partial class addUser : Form
{
MySqlConnection con;
MySqlDataAdapter da;
DataSet ds = new DataSet();
DataTable dt = new DataTable();
public addUser()
{
InitializeComponent();
string str = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\MARUBI\MARUBI\MARUBI\MarubiDB.mdf;Integrated Security=True;Connect Timeout=30";
con = new MySqlConnection(str);
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string username = textBox1.Text;
string password = textBox2.Text;
string name = textBox3.Text;
string surename = textBox4.Text;
string tel = textBox5.Text;
string email = textBox6.Text;
string sql = "INSERT INTO dbo.users(username, password, name, surename, tel, email) VALUES ('"+username+"', '"+password+"','"+name+"','"+surename+"','"+tel+"','"+email+"'";
try
{
con.Open();
da = new MySqlDataAdapter();
da.InsertCommand = new MySqlCommand(sql, con);
da.InsertCommand.ExecuteNonQuery();
MessageBox.Show("Values inserted succesfully");
load_grid();
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void load_grid()
{
dt.Clear();
MySqlDataAdapter da = new MySqlDataAdapter();
try
{
string sql = "SELECT * FROM USERS";
con.Open();
da.SelectCommand = new MySqlCommand(sql, con);
da.Fill(ds);
da.Fill(dt);
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void addUser_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'marubiDBDUSERS.users' table. You can move, or remove it, as needed.
load_grid();
}
}
这是我的代码,但出现错误
System.ArgumentException:'不支持该选项。
参数名称:attachdbfilename'
答案 0 :(得分:1)
您的C#代码使用MySQL驱动程序:
MySqlConnection con;
MySqlDataAdapter da;
但是您传递的连接字符串是与竞争对手的供应商(Microsoft SQL Server)完全不同的数据库系统的连接字符串:
"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=E:\MARUBI\MARUBI\MARUBI\MarubiDB.mdf"
使用正确的代码或正确的连接字符串;您没有说出您实际在使用哪个数据库