主题:[讨论]C#连接MYSQL
我在尝试用C#连接mysql数据可是没有成功
我用的是Microsoft.Data.Odbc并且装了mysql的odbc驱动
我做的登陆 可是!!!以下是代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.Odbc;
namespace TRWeb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
//DBHelper.connection.Open();
//string sql = string.Format("select count(*) from users where id='{0}' and loginpwd='{1}'", txt1.Text, txt2.Text);
//SqlCommand cmd = new SqlCommand(sql, DBHelper.connection);
//int n = (int)cmd.ExecuteScalar();
//DBHelper.connection.Close();
MySqlHelper.connection.Open();
//MessageBox.Show("打开!");
string sql = string.Format("select admingrade from admin where adminName={0} and adminPwd={1}",text1.Text,text2.Text);
//MessageBox.Show(sql);
OdbcCommand cmd = new OdbcCommand(sql,MySqlHelper.connection);
//MessageBox.Show("这里是:OdbcCommand cmd = new OdbcCommand(sql,MySqlHelper.connection);");
[color=FF0000][size=3]int n = (int)cmd.ExecuteScalar();[/size][/color]
MessageBox.Show("这里是:int n = (int)cmd.ExecuteScalar();");
MySqlHelper.connection.Close();
if (n == 1)
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("NO");
}
}
}
}
出错为红色字体部分
请大家指点一下
我用的是Microsoft.Data.Odbc并且装了mysql的odbc驱动
我做的登陆 可是!!!以下是代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Data.Odbc;
namespace TRWeb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void button1_Click(object sender, EventArgs e)
{
//DBHelper.connection.Open();
//string sql = string.Format("select count(*) from users where id='{0}' and loginpwd='{1}'", txt1.Text, txt2.Text);
//SqlCommand cmd = new SqlCommand(sql, DBHelper.connection);
//int n = (int)cmd.ExecuteScalar();
//DBHelper.connection.Close();
MySqlHelper.connection.Open();
//MessageBox.Show("打开!");
string sql = string.Format("select admingrade from admin where adminName={0} and adminPwd={1}",text1.Text,text2.Text);
//MessageBox.Show(sql);
OdbcCommand cmd = new OdbcCommand(sql,MySqlHelper.connection);
//MessageBox.Show("这里是:OdbcCommand cmd = new OdbcCommand(sql,MySqlHelper.connection);");
[color=FF0000][size=3]int n = (int)cmd.ExecuteScalar();[/size][/color]
MessageBox.Show("这里是:int n = (int)cmd.ExecuteScalar();");
MySqlHelper.connection.Close();
if (n == 1)
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("NO");
}
}
}
}
出错为红色字体部分
请大家指点一下