c# mysql 1062_C#中MySQL函数用DATASET 和 MySqlDataAdapter 操作数据库

[如果一门程序设计语言不支持数据库操作的话,其很难在现在的世界中存活下来。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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值