C#-mySQL连接程序,一个简单的数据库,1个表执行基本操作
我正在复制我的程序代码(VS2008)。我是一个初学者,我创建了一个数据库测试,其中一个表emp有3个字段e_name、e_id、age。调试完.cs文件后,我得到一个错误,因为-不包含适合入口点的静态“main”方法。请你帮我一下好吗 代码C#-mySQL连接程序,一个简单的数据库,1个表执行基本操作,c#,mysql,.net,C#,Mysql,.net,我正在复制我的程序代码(VS2008)。我是一个初学者,我创建了一个数据库测试,其中一个表emp有3个字段e_name、e_id、age。调试完.cs文件后,我得到一个错误,因为-不包含适合入口点的静态“main”方法。请你帮我一下好吗 代码 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Windows.Forms; 使用MySql.Data.MySqlClient; 名称空间ConnectCsharppToMySQL {
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Windows.Forms;
使用MySql.Data.MySqlClient;
名称空间ConnectCsharppToMySQL
{
公共类数据库连接
{
私有MySqlConnection;
私有字符串服务器;
私有字符串数据库;
私有字符串uid;
私有字符串密码;
//建造师
公共数据库连接()
{
初始化();
}
//初始化值
私有void初始化()
{
server=“localhost”;
database=“test”;
uid=“根”;
密码=”;
字符串连接字符串;
connectionString=“SERVER=“+SERVER+”;“+”数据库=”+
数据库+”;“+”UID=“+UID+”;“+”密码=“+PASSWORD+”;”;
连接=新的MySqlConnection(connectionString);
}
//打开到数据库的连接
私有bool OpenConnection()
{
尝试
{
connection.Open();
返回true;
}
捕获(MySqlException-ex)
{
//处理错误时,可以基于应用程序的响应
//关于错误号。
//连接时最常见的两个错误编号如下:
//0:无法连接到服务器。
//1045:用户名和/或密码无效。
开关(例如编号)
{
案例0:
MessageBox.Show(“无法连接到服务器。请与管理员联系”);
打破
案例1045:
MessageBox.Show(“无效的用户名/密码,请重试”);
打破
}
返回false;
}
}
//密切联系
私有布尔闭合连接()
{
尝试
{
connection.Close();
返回true;
}
捕获(MySqlException-ex)
{
MessageBox.Show(例如Message);
返回false;
}
}
//插入语句
公开作废插入()
{
string query=“插入emp(e_name,age)值('Pooja R','21')”;
//开放连接
if(this.OpenConnection()==true)
{
//创建命令并从构造函数中分配查询和连接
MySqlCommand cmd=新的MySqlCommand(查询、连接);
//执行命令
cmd.ExecuteNonQuery();
//密切联系
这个.CloseConnection();
}
}
//更新语句
公共无效更新()
{
string query=“UPDATE emp SET e_name='Peachy',age='22'其中name='Pooja R'”;
//开放连接
if(this.OpenConnection()==true)
{
//创建mysql命令
MySqlCommand cmd=新的MySqlCommand();
//使用CommandText分配查询
cmd.CommandText=查询;
//使用连接分配连接
cmd.Connection=连接;
//执行查询
cmd.ExecuteNonQuery();
//密切联系
这个.CloseConnection();
}
}
/*//删除语句
公共作废删除()
{
string query=“从tableinfo中删除,其中name='John Smith';
if(this.OpenConnection()==true)
{
MySqlCommand cmd=新的MySqlCommand(查询、连接);
cmd.ExecuteNonQuery();
这个.CloseConnection();
}
}
*/
//Select语句
公共列表[]选择()
{
string query=“从emp中选择*”;
//创建一个列表来存储结果
列表[]列表=新列表[3];
列表[0]=新列表();
列表[1]=新列表();
列表[2]=新列表();
//开放连接
if(this.OpenConnection()==true)
{
//创建命令
MySqlCommand cmd=新的MySqlCommand(查询、连接);
//创建数据读取器并执行命令
MySqlDataReader=cmd.ExecuteReader();
//读取数据并将其存储在列表中
while(dataReader.Read())
{
列表[0]。添加(dataReader[“e_id”]+”);
列表[1]。添加(dataReader[“e_name”]+);
列表[2]。添加(dataReader[“年龄”]+);
}
//关闭数据读取器
dataReader.Close();
//密切联系
这个.CloseConnection();
//要显示的返回列表
退货清单;
}
其他的
{
退货清单;
}
}
公共静态void main(字符串[]args)
{
DBConnect db1=新的DBConnect();
控制台写入线(“初始化”);
db1.Initialize();
WriteLine(“将记录插入表”);
db1.Insert();
Console.WriteLine(“更新记录”);
db1.Update();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace ConnectCsharppToMySQL
{
public class DBConnect
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
//Constructor
public DBConnect()
{
Initialize();
}
//Initialize values
private void Initialize()
{
server = "localhost";
database = "test";
uid = "root";
password = "";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
//open connection to database
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
//Insert statement
public void Insert()
{
string query = "INSERT INTO emp (e_name, age) VALUES('Pooja R', '21')";
//open connection
if (this.OpenConnection() == true)
{
//create command and assign the query and connection from the constructor
MySqlCommand cmd = new MySqlCommand(query, connection);
//Execute command
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}
//Update statement
public void Update()
{
string query = "UPDATE emp SET e_name='Peachy', age='22' WHERE name='Pooja R'";
//Open connection
if (this.OpenConnection() == true)
{
//create mysql command
MySqlCommand cmd = new MySqlCommand();
//Assign the query using CommandText
cmd.CommandText = query;
//Assign the connection using Connection
cmd.Connection = connection;
//Execute query
cmd.ExecuteNonQuery();
//close connection
this.CloseConnection();
}
}
/* //Delete statement
public void Delete()
{
string query = "DELETE FROM tableinfo WHERE name='John Smith'";
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.ExecuteNonQuery();
this.CloseConnection();
}
}
*/
//Select statement
public List<string>[] Select()
{
string query = "SELECT * FROM emp";
//Create a list to store the result
List<string>[] list = new List<string>[3];
list[0] = new List<string>();
list[1] = new List<string>();
list[2] = new List<string>();
//Open connection
if (this.OpenConnection() == true)
{
//Create Command
MySqlCommand cmd = new MySqlCommand(query, connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
list[0].Add(dataReader["e_id"] + "");
list[1].Add(dataReader["e_name"] + "");
list[2].Add(dataReader["age"] + "");
}
//close Data Reader
dataReader.Close();
//close Connection
this.CloseConnection();
//return list to be displayed
return list;
}
else
{
return list;
}
}
public static void main(String[] args)
{
DBConnect db1 = new DBConnect();
Console.WriteLine("Initializing");
db1.Initialize();
Console.WriteLine("Inserting a record into table");
db1.Insert();
Console.WriteLine("Updating the record");
db1.Update();
Console.WriteLine("Displaying the table :");
db1.Select();
}
}
}
public static void main(String[] args)
public static void Main(String[] args)
public static void Main(String[] args)
Console.WriteLine("Inserting a record into table");
Console.WriteLine("Inserting a record into table");
MessageBox.Show("Inserting a record into table");