推荐于2018-04-24 · 知道合伙人互联网行家
关注
展开全部
DataTable dt = new DataTable();
using (MySqlConnection con = new MySqlConnection())
{
con.ConnectionString = "Data Source=127.0.0.1;Initial Catalog=mysql;User ID=root;password=";
con.Open();
string query = string.Format("select id ,name ,image from AAA");
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
dt.Load(cmd.ExecuteReader());
}
}
dt.Columns.Add("图片",System.Type.GetType("System.Byte[]"));
DataTable dt2 = dt.Clone();
foreach (DataRow item in dt.Rows)
{
DataRow dr = dt2.NewRow();
dr[0] = item[0];
dr[1] = item[1];
dr[2] = item[2];
using (FileStream fs = new FileStream(item[2].ToString(), FileMode.Open))//根据路径取得图片
{
using (BinaryReader br = new BinaryReader(fs))
{
dr["图片"] = br.ReadBytes((int)fs.Length);
}
}
dt2.Rows.Add(dr);
}
dataGridView1.DataSource = dt2;
dataGridView1.Columns["图片"].DisplayIndex = 0;
dataGridView1.Columns["id"].DisplayIndex = 1;
dataGridView1.Columns["name"].DisplayIndex = 2;
dataGridView1.Columns["image"].Visible = false ;
using (MySqlConnection con = new MySqlConnection())
{
con.ConnectionString = "Data Source=127.0.0.1;Initial Catalog=mysql;User ID=root;password=";
con.Open();
string query = string.Format("select id ,name ,image from AAA");
using (MySqlCommand cmd = new MySqlCommand(query, con))
{
dt.Load(cmd.ExecuteReader());
}
}
dt.Columns.Add("图片",System.Type.GetType("System.Byte[]"));
DataTable dt2 = dt.Clone();
foreach (DataRow item in dt.Rows)
{
DataRow dr = dt2.NewRow();
dr[0] = item[0];
dr[1] = item[1];
dr[2] = item[2];
using (FileStream fs = new FileStream(item[2].ToString(), FileMode.Open))//根据路径取得图片
{
using (BinaryReader br = new BinaryReader(fs))
{
dr["图片"] = br.ReadBytes((int)fs.Length);
}
}
dt2.Rows.Add(dr);
}
dataGridView1.DataSource = dt2;
dataGridView1.Columns["图片"].DisplayIndex = 0;
dataGridView1.Columns["id"].DisplayIndex = 1;
dataGridView1.Columns["name"].DisplayIndex = 2;
dataGridView1.Columns["image"].Visible = false ;
展开全部
呵呵 无意中看到
http://pan.baidu.com/s/1hqn3UnU
最好下载安装
mysql.data.dll在路径C:\Program Files\MySQL\MySQL Connector Net 6.2.5\Assemblies文件夹下添加引用
---我已经有代码了
命名空间:
using MySql.Data.MySqlClient;
----------------
数据库操作方法
public void getconn(string sql)//数据库操作链接方法
{
string conn = ConfigurationSettings.AppSettings["conn"].ToString();
MySqlConnection mysql = new MySqlConnection(conn);//实例化链接
mysql.Open();//开启
MySqlCommand comm = new MySqlCommand(sql, mysql);
comm.ExecuteNonQuery();//执行
mysql.Close();//关闭资源
}
public static MySqlDataAdapter getadaoter(string sql)//显示操作
{
string conn = ConfigurationSettings.AppSettings["conn"].ToString();
MySqlConnection mysql = new MySqlConnection(conn);//实例化链接
mysql.Open();//开启
MySqlCommand comm = new MySqlCommand(sql, mysql);
comm.ExecuteNonQuery();
MySqlDataAdapter mda = new MySqlDataAdapter(sql, mysql);
mysql.Close();
return mda;
//需要在调用的时候进行数据集填充
}
添加应用程序配置文件APP.CONFIG中
------
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="conn" value="Server=112.0.1.1;User ID=root;Password=ssussdfdgdfodu;Database=st_goods_category;CharSet=gbk;"/>
<!--链接数据库-->
<add key="category" value="st_category"/>
</appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
--------
程序事件
private void button1_Click(object sender, EventArgs e)
{
try{
string sql="你的数据库查询结果";
dataGridView1.Rows.Clear();
DataSet ds = new DataSet();
getadaoter(sql).Fill(ds, "tbl_shop_category");
foreach (DataRow row1 in ds.Tables["tbl_shop_category"].Rows)
{
dataGridView1.Rows.Add(row1[i].ToString());
}
}
}
}
http://pan.baidu.com/s/1hqn3UnU
最好下载安装
mysql.data.dll在路径C:\Program Files\MySQL\MySQL Connector Net 6.2.5\Assemblies文件夹下添加引用
---我已经有代码了
命名空间:
using MySql.Data.MySqlClient;
----------------
数据库操作方法
public void getconn(string sql)//数据库操作链接方法
{
string conn = ConfigurationSettings.AppSettings["conn"].ToString();
MySqlConnection mysql = new MySqlConnection(conn);//实例化链接
mysql.Open();//开启
MySqlCommand comm = new MySqlCommand(sql, mysql);
comm.ExecuteNonQuery();//执行
mysql.Close();//关闭资源
}
public static MySqlDataAdapter getadaoter(string sql)//显示操作
{
string conn = ConfigurationSettings.AppSettings["conn"].ToString();
MySqlConnection mysql = new MySqlConnection(conn);//实例化链接
mysql.Open();//开启
MySqlCommand comm = new MySqlCommand(sql, mysql);
comm.ExecuteNonQuery();
MySqlDataAdapter mda = new MySqlDataAdapter(sql, mysql);
mysql.Close();
return mda;
//需要在调用的时候进行数据集填充
}
添加应用程序配置文件APP.CONFIG中
------
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="conn" value="Server=112.0.1.1;User ID=root;Password=ssussdfdgdfodu;Database=st_goods_category;CharSet=gbk;"/>
<!--链接数据库-->
<add key="category" value="st_category"/>
</appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
--------
程序事件
private void button1_Click(object sender, EventArgs e)
{
try{
string sql="你的数据库查询结果";
dataGridView1.Rows.Clear();
DataSet ds = new DataSet();
getadaoter(sql).Fill(ds, "tbl_shop_category");
foreach (DataRow row1 in ds.Tables["tbl_shop_category"].Rows)
{
dataGridView1.Rows.Add(row1[i].ToString());
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询