一、c#学习(二)–c#中MySqlCommand用法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Configuration;
namespace _01AdoNetCourse
{
class Program
{
static void Main(string[] args)
{
MySqlConnection connection=null;
try {
connection = new MySqlConnection();
string str = ConfigurationManager.ConnectionStrings["conStri"].ConnectionString;
connection.ConnectionString = str;
MySqlCommand sqlCommand = new MySqlCommand();
sqlCommand.Connection = connection;
sqlCommand.CommandText = "select * from news";
Console.WriteLine(connection.Database);
Console.WriteLine("连接成功");
connection.Open();
MySqlDataReader mySqlData= sqlCommand.ExecuteReader();
while (mySqlData.Read())
{
Console.WriteLine("id=" + mySqlData.GetString("id")+" name="+mySqlData.GetString("title")+" content="+mySqlData.GetString("content")+" insertdate="+mySqlData.GetDateTime("insertdate"));
}
connection.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
Console.ReadKey();
}
}
}
}