using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
namespace mouse
{
class conn
{
private MySqlConnection myConnection;
private MySqlCommand myCommand;
private MySqlDataAdapter myAdapter;
private MySqlTransaction myTransaction;
private MySqlConnection connect;
//建立DB连接
public void getConnection()
{
//StreamReader din = File.OpenText("TextFile.ini");
string contString = "datasource=localhost;username=root;password=;database=test;charset=utf8";
try
{
myConnection = new MySqlConnection();
myConnection.ConnectionString = contString;
myConnection.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex);
MessageBox.Show("连接失败!");
}
}
public DataSet GetDB(string sql)
//public Conn(string sql)
{
DataSet mySet = new DataSet();
//try
//{
string contString = "datasource=localhost;username=root;password=;database=test;charset=utf8";
//String SQL = "select * from user_table where username='" + tb_username.Text.Trim() + "'";
MySqlConnection conn = new MySqlConnection(contString);
MySqlCommand myCommand = new MySqlCommand();
myCommand.Connection = conn;
myCommand.CommandText = sql;
myAdapter = new MySqlDataAdapter(myCommand);
myAdapter.Fill(mySet);
return mySet;
//}
//catch (Exception ex)
//{
// Console.WriteLine(ex);
// mySet = new DataSet();
// MessageBox.Show("数据发生错误!");
// return mySet;
//}
}
public MySqlDataAdapter getda(string sql)
{
myCommand = myConnection.CreateCommand();
myCommand.CommandText = sql;
myAdapter = new MySqlDataAdapter(myCommand);
return myAdapter;
}
public void insertdb(string sql)
//public Conn(string sql)
{
string contString = "datasource=localhost;username=root;password=;database=test;charset=utf8";
//String SQL = "select * from user_table where username='" + tb_username.Text.Trim() + "'";
MySqlConnection conn = new MySqlConnection(contString);
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;
cmd.CommandText = sql;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
public MySqlCommandBuilder cb = new MySqlCommandBuilder();
public MySqlDataAdapter da = new MySqlDataAdapter();
public void save(string sql, DataSet ds, MySqlDataAdapter da)
//public Conn(string sql)
{
string contString = "datasource=localhost;username=root;password=;database=test;charset=utf8";
//String SQL = "select * from user_table where username='" + tb_username.Text.Trim() + "'";
MySqlConnection conn = new MySqlConnection(contString);
//OleDbDataAdapter adapter = new OleDbDataAdapter(sql, conn);
MySqlCommand cmd = new MySqlCommand();
cmd = conn.CreateCommand();
cmd.CommandText = sql;
da = new MySqlDataAdapter(cmd);
cb = new MySqlCommandBuilder(da);
da.Update(ds);
}
public bool chkDB(string str)
{
int s = int.Parse(str.ToString());
string sql = "select ID from product where 商品编号 =\""+s+"\"";
DataSet ds_chk=GetDB(sql);
if (ds_chk.Tables[0].Rows.Count ==0)
{
return true;
}
else
{
return false;
}
}
public string sjc(DateTime times)
{
DateTime aa = new DateTime(1970, 1, 1);
long b = (times.Ticks - aa.Ticks) / 10000000 - 8 * 60 * 60;
return b.ToString();
}
public void bc(string sql,DataSet ds)
{
string contString = "datasource=localhost;username=root;password=;database=test;charset=utf8";
MySqlCommand cmd = new MySqlCommand();
connect = new MySqlConnection(contString);
cmd = connect.CreateCommand();
cmd.CommandText = sql;
da = new MySqlDataAdapter(cmd);
cb = new MySqlCommandBuilder(da);
da.Update(ds);
}
}
}
c#与mysql连接类
精选 转载下一篇:C#动态数组实用实例解析
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
MySQL中表连接的常用技巧
MySQL中表连接的常用技巧
MySQL 表连接 右连接 -
C# mysql 连接字符串
server=127.0.0.1;database=world;userid=sa;password=passwordserver=电脑ip
C# mysql 连接字符串 mysql 数据库 连接字符串 -
【C#】【MySQL】C#连接MySQL数据库(一)代码
本文大致讲了C#如何使用MySQL,附上了代码,但是解析是另外一篇博文,链接见本文文末 ...
ASP.NET MySQL C# mysql html