c#数据库操做Demo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace ConsoleApplication1
{
class Program
{ //数据库执行操做类
private SqlCommand cmd = null;
//数据库链接操做类
private SqlConnection mycon = null;
//链接参数
private string conn = "Server=localhost ;Database=IntelligentCity_2015_GZ ;user id=sa;password=123456";
//链接sql
private string sql = "select * from ASM_ACCOUNT";
static void Main(string[] args)
{
Program pr = new Program();
pr.getConnected();
}
//获取链接
private void getConnected()
{
try
{
mycon = new SqlConnection(conn);
mycon.Open();
//调用操做数据库的方法,读取数据库中的数据
operationDatabase(mycon);
}catch(Exception e)
{
Console.WriteLine(e);
Console.ReadKey();
}finally
{
mycon.Close();
}
}
//数据库操做方法,读取数据
private void operationDatabase(SqlConnection conn )
{
cmd = new SqlCommand( sql,conn );
//cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
Console.WriteLine("{0},{1}", reader["ACT_LOGIN_NAME"], reader["ACT_LOGIN_PASSWORD"]);
Console.ReadKey();
}
reader.Close();
}
}
}
