asp教程.net c#取excel 合并单元格内容
读取excel数据,填充dataset
// 连接字符串
string xlspath = server.mappath(“~/www.111cn.net/somefile.xls”);
string connstr = “provider=microsoft.jet.oledb.4.0;” +
“extended properties=”excel 8.0;hdr=no;imex=1″;” + // 指定扩展属性为
microsoft excel 8.0 9.0 10.0
,并且第一行作为数据返回,且以文本方式读取
“data source=” + xlspath;
string sql_f = “select * from [{0}]”;

关于c#中多种连接mysql教程数据方法
方法一:
使用mysql推出的mysql connector/net is an ado.net driver for mysql
该组件为mysql为ado.net访问mysql数据库教程设计的.net访问组件。
安装完成该组件后,引用命名空间mysql.data.mysqlclient;
使用命令行编译时:csc /r:mysql.data.dll test.cs
方法二:
通过odbc访问mysql数据库
访问前要先下载两个组件:odbc.net和mysql的odbc驱动(mysql connector/odbc
(myodbc) driver)目前为3.51版
安装完成后,即可通过odbc访问mysql数据库
方法三:
使用corelab推出的mysql访问组件,面向.net
安装完成后,引用命名空间:corelab.mysql;
使用命令编译时:csc /r:corelab.mysql.dll test.cs

oledbconnection conn = null;
oledbdataadapter da = null;
datatable tblschema = null;
ilist<string> tblnames = null;

mysqlconnection conn = null;   
 try  
 {   
//mysqlconnection conn = new mysqlconnection(new
mysqlconnectionstring(“myquest”, “root”, “amttgroup”).asstring);   
//实例化一个连接对象其中myquest为数据库名,root为数据库用户名,amttgroup为数据库密码    
     string connstr = “data source=mysql;password=root;user
id=root;location=localhost”;   
     conn =     new mysqlconnection(constr);   
     conn.open();   
     string query = “insert into test.dbtable values(10,
‘disksidkfsdi’, ‘asdfaf’, ‘adsfasdf’)”;   
     string tmp = null;   
     mysqlcommand cmd = new mysqlcommand(query, conn);   
     for(int i = 0; i < 100000; i++)   
     {   
       cmd.executenonquery();   
     }   
     cmd.dispose();   
     conn.close();   
     query = “select * from test.dbtable”;   
     mysqlcommand cmd2 = new mysqlcommand(query, conn);   
     conn.open();   
  
  //设定中文输出   
  mysqlcommand firstcmd = new mysqlcommand(“set names gb2312”,
myconn);   
  firstcmd.executenonquery();   
  
     mysqldatareader reader = cmd2.executereaderex();   
     while(reader.read())   
     {   
         tmp = reader[0].tostring();   
         tmp = reader[1].tostring();   
         tmp = reader[2].tostring();   
         tmp = reader[3].tostring();   
     }   
     conn.close();   
     query = “delete from test.dbtable”;   
     mysqlcommand cmd3 = new mysqlcommand(query, conn);   
     conn.open();   
     cmd3.executenonquery();   
 }   
 catch(exception ex)   
 {   
     messagebox.show(ex.message);   
 }   
 finally  
 {   
     conn.close();   
 }

// 初始化连接,并打开
conn = new oledbconnection;
conn.open();

mysqldrivercs实现在.net环境下访问mysql数据库

// 获取数据源的表定义元数据
//tblschema = conn.getschema;
tblschema = conn.getoledbschematable(oledbschemaguid.tables, new
object[] { null, null, null, “table” });

 

//gridview1.datasource = tblschema;
//gridview1.databind();

using system;

using system.data;

using system.data.sqlclient;

using system.configuration;

using system.web;

using system.web.security;

using system.web.ui;

using system.web.ui.webcontrols;

using system.web.ui.webcontrols.webparts;

using system.web.ui.htmlcontrols;

using mysqldrivercs;

 

public partial class _default : system.web.ui.page

{

    protected void page_load(object sender, eventargs e)

    {

        mysqlconnection conn = null;

        conn = new mysqlconnection(new
mysqlconnectionstring(“127.0.0.1”, “dvbbs”, “root”,
“123456”).asstring);

        conn.open();

        mysqlcommand commn = new mysqlcommand(“set names
gb2312”,conn);

        commn.executenonquery();

        string sql = “select title,topicid, boardid ,dateandtime,hits 
from  dv_topic order by dateandtime desc limit 10”;

        mysqldataadapter mda = new mysqldataadapter(sql,conn);

        dataset ds = new dataset();

        mda.fill(ds, “table1”);

        this.repeater1.datasource = ds;

        this.repeater1.databind();

        conn.close();

    }

}

// 关闭连接
//conn.close();

就这样就可以实现在.net环境下对mysql数据的访问。在这之前还要做一些准备,下载mysqldrivercs,安装,在安装文件夹下面找到mysqldriver.dll,然后将mysqldriver.dll添加引用到项目中就ok了。也许你对这行代码

tblnames = new list<string>();
foreach (datarow row in tblschema.rows) {
tblnames.addrow[“table_name”]); // 读取表名
}

mysqlcommand commn = new mysqlcommand(“set names gb2312”,conn);

// 初始化适配器
da = new oledbdataadapter();
// 准备数据,导入dataset
dataset ds = new dataset();

        commn.executenonquery();

foreach (string tblname in tblnames) {
da.selectcommand = new oledbcommand(string.format(sql_f, tblname),
conn);
try {
da.fill(ds, tblname);
}
catch {
// 关闭连接
if (conn.state == connectionstate.open) {
conn.close();
}
throw;
}
}

网友提供的方法

发表评论

电子邮件地址不会被公开。 必填项已用*标注