C#串連Access資料庫,C#串連Excel資料庫,C#串連SqlServer資料庫,C#串連Mysql資料庫總結

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.