標籤:c#串連資料庫
大二下學期寫的了,如今畢業一個月了,整理整理
額,以前寫的好幼稚,只有自己明白了,網上一大堆,大概的意思說下吧
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