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();
}
}