标题:vs2008连sqlserver2008,请高手指点
取消只看楼主
outman000
Rank: 1
等 级:新手上路
帖 子:33
专家分:0
注 册:2010-10-29
结帖率:55.56%
 问题点数:0 回复次数:0 
vs2008连sqlserver2008,请高手指点
程序代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string strcon = @"server=localhost;database=Test;uid=sa;pwd=sa";
                SqlConnection sqlconn = new SqlConnection(strcon);
                SqlCommand sqlcomm = sqlconn.CreateCommand();
                string commStr = "select * from student";
                 = commStr;
                sqlconn.Open();
                SqlDataReader myDateReader = sqlcomm.ExecuteReader();
                myDateReader.Read();
                Console.WriteLine("asdfasd");
                Console.ReadLine();
                sqlconn.Close();
            }
            catch (Exception e)
            {
                // MessageBox.Show("连接错误" + e.ToString());
                Console.WriteLine(e.ToString());
            }
            finally
            {
                Console.ReadLine();
            }
        }
    }
}


运行结果
System.Data.SqlClient.SqlException (0x80131904): 用户 'sa' 登录失败。原因: 未与
信任 SQL Server 连接相关联。
   在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception
, Boolean breakConnection)
   在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cm
dHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, Tds
ParserStateObject stateObj)
   在 System.Data.SqlClient.(Boolean enlis
tOK)
   在 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo
serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeo
ut, SqlConnection owningObject)
   在 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo
serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection ow
ningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
   在 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnecti
on owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, St
ring newPassword, Boolean redirectedUserInstance)
   在 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdent
ity identity, SqlConnectionString connectionOptions, Object providerInfo, String

 newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   在 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOp
tions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection

 owningConnection)
   在 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConn
ection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   在 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owning
Object)
   在 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection o
wningObject)
   在 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection ownin
gObject)
   在 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection ow
ningConnection)
   在 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection ou
terConnection, DbConnectionFactory connectionFactory)
   在 System.Data.SqlClient.SqlConnection.Open()
   在 ConsoleApplication5.Program.Main(String[] args) 位置 D:\C#workspace\Consol
eApplication6\ConsoleApplication6\Program.cs:行号 19


老是连不上,但是绑定可以,大家帮忙看看
2011-05-21 11:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-340613-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.085505 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved