程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> windows平台下MySQL的C#基本操作

windows平台下MySQL的C#基本操作

編輯:C#入門知識

Windows下使用MySQL時候注意事項:

      1:下載MYSQL,安裝配置的時候使用 GB2312編碼 ,  以免中文亂碼!

      2:下載相應的驅動MySQLDriver  並且安裝

      3:編寫代碼的時候設置引用MySQLDriverCS.dll  !

//***************************************************************************************

在MYSQL查看數據:--->

\

<form id="form1" runat="server">
    <div>  </div>
        <asp:TextBox ID="tbName" runat="server"></asp:TextBox>
        <asp:TextBox ID="tbAddress" runat="server"></asp:TextBox>
        <asp:TextBox ID="tbTelephone" runat="server"></asp:TextBox>
        <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="insert data" /><br />
    <div>
        <asp:GridView ID="gwShow" runat="server">
        </asp:GridView>
    </div>
    </form>

 

 

//**************
using MySQLDriverCS;
 

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData();
    }

    private void BindData()
    {
        MySQLConnection conn = null; 
        conn = new MySQLConnection(new MySQLConnectionString("localhost", "MyTest", "root", "jasenkin").AsString);

                                                                                             //本地數據庫  數據庫名字  用戶名root  密碼
        conn.Open();
        MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
        commn.ExecuteNonQuery();       //設置set names gb2312   解決亂碼
        string sql = "select * from user"; 
        MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        mda.Fill(ds, "tb"); 
        this.gwShow.DataSource = ds.Tables["tb"];
        gwShow.DataBind();
        conn.Close();  
    }


    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        MySQLConnection conn = null;
        conn = new MySQLConnection(new MySQLConnectionString("localhost", "MyTest", "root", "jasenkin").AsString);
        conn.Open();
        MySQLCommand commn = new MySQLCommand("set names gb2312", conn);
       commn.ExecuteNonQuery();
        MySQLCommand comm = new MySQLCommand(string.Format("insert into user (name,address,telephone) values ({0},{1},{2})", tbName.Text.Trim(), tbAddress.Text.Trim(), tbTelephone.Text.Trim()), conn);
        comm.ExecuteNonQuery();
        conn.Close();
        BindData();
    }
}
 

 

    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved