文章来源:网络
using System;
using
System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySQLDriverCS;
using System.Data;
using System.Configuration;
public partial class _Default :
System.Web.UI.Page
{
protected
void Page_Load(object sender, EventArgs e)
{
BindData();
}
private void
BindData()
{
string constr =
ConfigurationManager.AppSettings["JICQMySql"];
MySQLConnection conn = null;
// conn = new MySQLConnection(new
MySQLConnectionString("localhost", "protege_db", "root",
"sdie?!3406").AsString);
conn=new MySQLConnection(constr);
//本地数据库 数据库名字 用户名root 密码
conn.Open();
MySQLCommand commn = new MySQLCommand("set names gb2312",
conn);
commn.ExecuteNonQuery();
//设置set names gb2312
解决乱码
string sql = "select * from animal";
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",
"protege_db", "root", "sdie?!3406").AsString);
conn.Open();
MySQLCommand commn = new MySQLCommand("set names gb2312",
conn);
commn.ExecuteNonQuery();
MySQLCommand comm = new MySQLCommand(string.Format("insert into
animal
(frame,frame_type,slot,facet,is_template,value_index,value_type)
values ('{0}','{1}','{2}',1,1,1,1)", tbName.Text.Trim(),
tbAddress.Text.Trim(), tbTelephone.Text.Trim()), conn);
comm.ExecuteNonQuery();
conn.Close();
BindData();
}