当前位置:数据库 > SQLServer >>

windows平台下MySQL的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();
    }
}
 

 

    
补充:软件开发 , C# ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,