C#连接Access数据库,C#连接Excel数据库,C#连接SqlServer数据库,C#连接Mysql数据库总结
大二下学期写的了,如今毕业一个月了,整理整理
额,以前写的好幼稚,只有自己明白了,网上一大堆,大概的意思说下吧
MySQL=========================================
http://user.qzone.qq.com/652768664/blog/1347859952
连接mysql需要安装mysql,在mysql的C盘安装目录有一个连接 lib,将改lib添加引用用项目OK;
static void Main(string[] args) { List<string> lstdb =MySqlConn(); foreach (string s in lstdb) { Console.WriteLine(s); } Console.Read(); } public static List<string> MySqlConn() { List<string> lstconn = new List<string>(); MySqlConnection objConn = new MySqlConnection(); objConn.ConnectionString = "server=127.0.0.1;User Id=root;Password=000000;Persist Security Info=True;database=test "; objConn.Open(); MySqlCommand objCmd = new MySqlCommand("select * from maya", objConn); MySqlDataReader dr = objCmd.ExecuteReader(); while (dr.Read()) { lstconn.Add(dr[0].ToString()); } objConn.Close(); return lstconn; } }
Access==========================================
http://user.qzone.qq.com/652768664/blog/1334636397
access2007 和 access 2003的连接字符串不同,一个ADO版本12.0,一个是4.0;
1,
public List<string> Access2007(string accdbpath,string tablename) { List<string> lstdata = new List<string>(); string strconn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + accdbpath + "'"; OleDbConnection conn = new OleDbConnection(strconn); conn.Open(); string cmdtext = "select * from " + tablename+""; OleDbDataAdapter apdate = new OleDbDataAdapter(cmdtext, conn); DataTable table = new DataTable(); apdate.Fill(table); if (table.Rows.Count > 0) { for (int irow = 0; irow < table.Rows.Count; irow++) for (int icol = 0; icol < table.Columns.Count; icol++) lstdata.Add("Row:" + irow + " Column:" + icol + " ColumnName:" + table.Columns[icol].ColumnName + " Value:" + table.Rows[irow][table.Columns[icol].ColumnName]); } return lstdata; }2,
namespace 留言板_access数据库_ { public partial class _Default : System.Web.UI.Page { string strConnection = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=F:\\留言板.mdb "; string sex; protected void Page_Load(object sender, EventArgs e) { OleDbConnection coon = new OleDbConnection(strConnection); coon.Open(); Response.Write("Access数据库连接状态显示:" + coon.State); coon.Close(); Label1.Text=DateTime.Now.ToString(); if (RadioButton1.Checked) { sex = "男"; } else { sex = "女"; } } protected void Button1_Click(object sender, EventArgs e) { TextBox2.Text = ""; } public void Seek(string str) { using (OleDbConnection coon = new OleDbConnection(strConnection)) { OleDbCommand cmd = new OleDbCommand(str, coon); try { coon.Open(); OleDbDataReader r = cmd.ExecuteReader(); while (r.Read()) TextBox3.Text = string.Format("留言人:{0};性别:{1};留言内容:{2};留言时间{3}", r[0], r[1], r[2],r[4]); } catch (Exception ee) { Response.Write(ee.Message); } } } public void Insert(string str) { using (OleDbConnection coon = new OleDbConnection(strConnection)) { OleDbCommand cmd = new OleDbCommand(str, coon); try { coon.Open(); cmd.ExecuteNonQuery(); Response.Write("<script>window.alert('数据添加成功!');</script>"); } catch (Exception ee) { Response.Write(ee.Message); } } } protected void Button2_Click(object sender, EventArgs e) { Seek("select * from 留言板 where 姓名= '" + TextBox1.Text.Trim() + "'"); } protected void Button3_Click(object sender, EventArgs e) { Insert("insert into 留言板(姓名,性别,留言内容,留言时间) values('"+TextBox1.Text.Trim()+"','"+sex.Trim()+"','"+TextBox2.Text.Trim()+"','"+Label1.Text+"')"); Button2_Click(sender,e); } } }
Excel==========================================
http://user.qzone.qq.com/652768664/blog/1350139919
class ExcelInAndOut
{
/// 读取Excel文档
/// </summary>
/// <param name="Path">文件名称</param>
/// <returns>返回一个数据集</returns>
public DataSet ExcelToDS(string Path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel = "select * from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommand.Fill(ds, "table1");
return ds;
}
}
SqlServer and SqlLite==========================================
一个服务器名为 . 一个服务器名为 .\sqlexpress
C#连接Access数据库,C#连接Excel数据库,C#连接SqlServer数据库,C#连接Mysql数据库总结,古老的榕树,5-wow.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。