加入收藏 | 设为首页 | 会员中心 | 我要投稿 晋中站长网 (https://www.0354zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

c mysql dataset查询_C#中MySQL函数用DATASET 和 M

发布时间:2022-10-18 14:31:13 所属栏目:MySql教程 来源:未知
导读: 1.C#中调用MYSQL数据库时,我用的是MySQLDriverCS这个方法.
一般的查询可以方便执
String connStr, commStr;
DataSet ds; //数据集
BindingSource bs; //数据绑定源,注意是全局的变量
MySQ

1.C#中调用MYSQL数据库时,我用的是MySQLDriverCS这个方法.

一般的查询可以方便执

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 "

//---

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);

}

(编辑:晋中站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!