日期:2014-05-17 浏览次数:20494 次
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace 数据库远程连接练习45
{
class Program
{
static void Main(string[] args)
{
string connection = @"server=192.168.0.135 ; database=lianxi ; user=*** ; password=*****";
string sqlCMD = @"select * from goods";
SqlConnection cn = new SqlConnection(connection);
try
{
cn.Open();
Console.WriteLine("ok");
SqlCommand cmd = new SqlCommand(sqlCMD, cn);
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Console.WriteLine("name:{0} price:{1}", reader["name"], reader["price"]);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
cn.Close();
}
Console.Read();
}
}
}