//建立链接字符串
string connString = @"server = .\sqlexpress;
integrated security = true;
database = northwind";
这个是书上的;想知道 server = 后面的是什么东西??
就.\sqlexpress是个什么东西,书上没说明白,请教高手!!
sing System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;namespace Chapter04
{
class SqlServerProvider
{
static void Main(string[] args)
{
//建立链接字符串
string connString = @"server = .\sqlexpress;
integrated security = true;
database = northwind"; //建立查询字符串
string sql = @"selsct
*
from
employess
";
//声明链接变量和数据读取器变量
SqlConnection conn = null;
SqlDataReader reader = null; try
{
//创建连接并打开它
conn = new SqlConnection(connString);
conn.Open(); //创建命令对象
SqlCommand cmd = new SqlCommand(sql, conn);
//建立数据读取器并执行查询
reader = cmd.ExecuteReader(); //显示标题
Console.WriteLine("this progream demonstrates the use of the SQL Server Data Provider.");
Console.WriteLine("Querying database {0} with query {1}\n", conn.Database, cmd.CommandText);
Console.WriteLine("First Name\t Last Name\n"); //调用读取数据Read的方法
while (reader.Read())
{
Console.WriteLine("{0}|{1}", reader["FirstName"].ToString().PadLeft(10), reader[1].ToString().PadLeft(10));
}
}
catch (Exception e)
{
Console.WriteLine("Error:" + e);
}
finally
{ //关闭 connection
reader.Close();
conn.Close();
} }
}
}
有空的话顺便帮忙看下代码有没有问题,只要在控制台应用程序中写入就可以运行了的吧,不需要做额外的手动连接吧可是运行有问题出来是空白!!
string connString = @"server = .\sqlexpress;
integrated security = true;
database = northwind";
这个是书上的;想知道 server = 后面的是什么东西??
就.\sqlexpress是个什么东西,书上没说明白,请教高手!!
sing System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;namespace Chapter04
{
class SqlServerProvider
{
static void Main(string[] args)
{
//建立链接字符串
string connString = @"server = .\sqlexpress;
integrated security = true;
database = northwind"; //建立查询字符串
string sql = @"selsct
*
from
employess
";
//声明链接变量和数据读取器变量
SqlConnection conn = null;
SqlDataReader reader = null; try
{
//创建连接并打开它
conn = new SqlConnection(connString);
conn.Open(); //创建命令对象
SqlCommand cmd = new SqlCommand(sql, conn);
//建立数据读取器并执行查询
reader = cmd.ExecuteReader(); //显示标题
Console.WriteLine("this progream demonstrates the use of the SQL Server Data Provider.");
Console.WriteLine("Querying database {0} with query {1}\n", conn.Database, cmd.CommandText);
Console.WriteLine("First Name\t Last Name\n"); //调用读取数据Read的方法
while (reader.Read())
{
Console.WriteLine("{0}|{1}", reader["FirstName"].ToString().PadLeft(10), reader[1].ToString().PadLeft(10));
}
}
catch (Exception e)
{
Console.WriteLine("Error:" + e);
}
finally
{ //关闭 connection
reader.Close();
conn.Close();
} }
}
}
有空的话顺便帮忙看下代码有没有问题,只要在控制台应用程序中写入就可以运行了的吧,不需要做额外的手动连接吧可是运行有问题出来是空白!!
.代表本机
\sqlexpress代表是一组数据库(VS2005自动创建的)"运行有问题出来是空白",程序问题,有报错或异常吗?
作为server参数
integrated security = true;
database = northwind";
server是服务器的名称,
database 数据库名,此外还有用户名和用户密码的。
\Sqlexpress是数据库的实例