连接到Sql Server 2014数据库时出错 [英] Error in connecting to Sql Server 2014 database

查看:111
本文介绍了连接到Sql Server 2014数据库时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server 2014中有一个数据库,并希望从数据库中的一个表中的一列填充一个ComboBox。以下是我用它做的代码:

  string  connectionString = ; 
SqlConnection连接;
SqlCommand命令;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
string sql = null ;
connectionString = 数据源= IMRAN-PC;初始目录= MyFirstSqlDbDataSet; Integrated Security = true;
sql = select * from CourseInfo;

connection = new SqlConnection(connectionString);

尝试
{
connection.Open();
command = new SqlCommand(sql,connection);
adapter.SelectCommand = command;
adapter.Fill(ds);
adapter.Dispose();
command.Dispose();
connection.Close();

cboCourse.DataSource = ds.Tables [ 0 ];
cboCourse.ValueMember = ID;
cboCourse.DisplayMember = [课程编号和名称];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, 连接错误);
}



问题是,当我运行应用程序时,会出现以下错误:

无法打开数据库MyFirstSqlDbDataSet 登录请求。登录失败。

用户'imran-PC\Anas'登录失败。



任何人都可以告诉我这个问题的原因以及这个问题的可能解决方案?

我还想知道如果使用Sql Server身份验证,必须在连接字符串中输入哪个用户名和密码。 Thanx提前寻求帮助。

解决方案

1.要创建数据库用户凭据(用户名和密码),必须使用SQL Management Studio并首先创建SQL Server登录帐户(整个SQL服务器的用户),然后进入数据库的安全部分,使用创建的登录帐户创建数据库用户,最后为创建的数据库用户授予DBO权限。



2.下一步是更改新创建的数据库用户凭据的连接字符串,如下例所示:

 connectionString =   server = ServerName; database = MyFirstSqlDbDataSet; user = Anas; password = ChangeMe123!; 



您可以看到上面的ServerName是您的SQL Server实例的值(并且也可以在您的LAN中作为IP提供),数据库用户名是Anas及其psssword是ChangeMe123!。



3.要创建数据库登录帐户和数据库凭据,您还可以使用SQL命令,如下一个示例:

 创建登录[Anas]  WITH  PASSWORD = N '  ChangeMe123!',DEFAULT_DATABASE = [MyFirstSqlDbDataSet],DEFAULT_LANGUAGE = [us_english],CHECK_EXPIRATION = OFF,CHECK_POLICY = OFF 
GO
USE [MyFirstSqlDbDataSet]
GO
CREATE USER [Anas] FOR LOGIN [Anas] WITH DEFAULT_ SCHEMA = [dbo]
EXEC sp_addrolemember ' db_datareader'' Anas'
EXEC sp_addrolemember ' db_datawriter' ' Anas'
GO


I have a database in SQL Server 2014 and want to fill a ComboBox from one column from one of the tables in the database. Here is the code with which I did it:

string connectionString = null;
        SqlConnection connection;
        SqlCommand command;
        SqlDataAdapter adapter = new SqlDataAdapter();
        DataSet ds = new DataSet();
        string sql = null;
connectionString = "Data Source=IMRAN-PC;Initial Catalog=MyFirstSqlDbDataSet;Integrated Security=true";
            sql = "select * from CourseInfo";

            connection = new SqlConnection(connectionString);

            try
            {
                connection.Open();
                command = new SqlCommand(sql, connection);
                adapter.SelectCommand = command;
                adapter.Fill(ds);
                adapter.Dispose();
                command.Dispose();
                connection.Close();

                cboCourse.DataSource = ds.Tables[0];
                cboCourse.ValueMember = "ID";
                cboCourse.DisplayMember = "[Course Number and Name]";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Connection Error");
            }


The problem is, when I run the application, the following error is given:
"Cannot open database "MyFirstSqlDbDataSet" requested by the login. The login failed.
Login failed for user 'imran-PC\Anas'."

Can anyone tell me the reason for this problem and the possible solution for this problem?
I also want to know which username and password have to be entered in connection string if using Sql Server authentication. Thanx in advance for help.

解决方案

1.For creating DB user credentials (user name and password) you have to use SQL Management Studio and first create a SQL Server login account (user for the entire SQL server), then go into the Security Section of your database an use the created login account to create a user of your database, finally give DBO rights to the created DB user.

2.The next step is to change the connection string for the new created DB user credentials like in the next example:

connectionString = "server=ServerName;database=MyFirstSqlDbDataSet;user=Anas;password=ChangeMe123!"; 


You can see above that the "ServerName"is the value for your SQL Server Instance (and could be also given as IP in your LAN), the DB user name is "Anas" and its psssword is "ChangeMe123!".

3.To create the DB login account and DB credentials you can use also SQL commands like in the next example:

CREATE LOGIN [Anas] WITH PASSWORD=N'ChangeMe123!', DEFAULT_DATABASE=[MyFirstSqlDbDataSet], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF
GO
USE [MyFirstSqlDbDataSet]
GO
CREATE USER [Anas] FOR LOGIN [Anas] WITH DEFAULT_SCHEMA=[dbo]
EXEC sp_addrolemember 'db_datareader', 'Anas'
EXEC sp_addrolemember 'db_datawriter', 'Anas'
GO


这篇关于连接到Sql Server 2014数据库时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆