乐乐

乐乐的博客园
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

SqlClient 连接 (System.Data.SqlClient)

Posted on 2007-07-18 15:25  带你去月球  阅读(2090)  评论(0编辑  收藏  举报
private static void ReadOrderData(string connectionString)
{
    
string queryString = 
        
"SELECT OrderID, CustomerID FROM dbo.Orders;";
    
using (SqlConnection connection = new SqlConnection(
               connectionString))
    
{
        SqlCommand command 
= new SqlCommand(
            queryString, connection);
        connection.Open();
        SqlDataReader reader 
= command.ExecuteReader();
        
try
        
{
            
while (reader.Read())
            
{
                Console.WriteLine(String.Format(
"{0}, {1}",
                    reader[
0], reader[1]));
            }

        }

        
finally
        
{
            
// Always call Close when done reading.
            reader.Close();
        }

    }

}


connectionString= "Data Source=serverName\instanceName;Initial Catalog=DatabaseName;Integrated Security=True";

Integrated Security : 集成windows验证。

使用 Windows 身份验证:

我们建议您使用 Windows 身份验证(通常称为集成安全性)连接到服务器数据库上。要指定 Windows 身份验证,可以对 SQL Server .NET Framework 数据提供程序使用下列两个键
/值对中的任意一个:

Integrated Security
=true;
Integrated Security
=SSPI;

connection string :http://www.connectionstrings.com/
要连接到 Microsoft SQL Server 7.0 或更高版本,请使用 SQL Server .NET Framework 数据提供程序的 SqlConnection 对象。要连接到 OLE DB 数据源,或连接到 Microsoft SQL Server 6.x 或更低版本,请使用 OLE DB .NET Framework 数据提供程序的 OleDbConnection 对象。要连接到 ODBC 数据源,请使用 ODBC .NET Framework 数据提供程序的 OdbcConnection 对象。要连接到 ODBC 数据源,请使用 Oracle .NET Framework 数据提供程序的 OracleConnection 对象。要安全地存储和检索连接字符串,请参见保护连接字符串。

无觅相关文章插件,快速提升流量