[如果一门程序设计语言不支持数据库操作的话,其很难在现在的世界中存活下来。C#语言提供了丰富的数据库操作类库,极大地方便了对数据库的操作。在C#中,常用的有三种 访问
1.C#中调用MYSQL数据库时,我用的是MySQLDriverCS这个方法.
一般的查询可以方便执
[string dbPath = @"Data\dbTest.mdb";string db = Server.MapPath(dbPath);string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + db;stri
String connStr, commStr;
DataSet ds; //数据集
BindingSource bs; //数据绑定源,注意是全局的变量
MySQLConnection myconn;
MySQLDataAdapter myadp;//数据适配器
connStr = "Data Source=myshiyandb;Password=110810;User ID=root;Location=localhost;Port=3306;Extended Properties=";//数据库连接字符串
//myconn = new MySQLConnection(new MySQLConnectionString("localhost", "myshiyandb", "root", "110810").AsString);
myconn = new MySQLConnection(connStr);
try
{
myconn.Open();//打开数据库
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
//
ds = new DataSet();
commStr = "select * from table1;";
myadp = new MySQLDataAdapter(commStr , myconn); //适配器
myadp.Fill(ds,"table"); //将查询到数据填充到数据集
bs = new BindingSource();
bs.DataSource = ds.Tables["table"];
dataGridView1.DataSource = bs; //绑定DataGridView到DataSet
//shut
try
{
myconn.Close();
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
2.但是在通过这种方法,改一下其中的sql语句,虽然程序执行了,但是记录添加不到数据库中;解决办法
connStr = "Data Source=myshiyandb;Password=110810;User ID=root;Location=localhost;Port=3306;Extended Properties=";//数据库连接字符串
//myconn = new MySQLConnection(new MySQLConnectionString("localhost", "myshiyandb", "root", "110810").AsString);
myconn = new MySQLConnection(connStr);
try
{
myconn.Open();//打开数据库
}
catch (Exception e2)
{
MessageBox.Show(e2.Message);
}
string strInsert = null;
strInsert = " insert into " + "table1" + " values "
+ " ( "
+ "'" + tbxLuruName.Text + "'" + ", "
+ tbxLuruAge.Text
+ " ) ";
//---
commStr = strInsert;
MySQLCommand cmd = new MySQLCommand(commStr, myconn);
cmd.ExecuteNonQuery();
//myadp = new MySQLDataAdapter(commStr, myconn); //适配器
try
{
myconn.Close();
}
catch (Exception e3)
{
MessageBox.Show(e3.Message);
}
[一.启动服务在已经配置环境变量的情况下,在命令行输入net start mysql启动MYSQL服务二.进入数据库操作命令行mysql -u root -p三.创建数据库创建数据库基础命令create dat