如何在visual studio中与数据库SQL服务器建立连接 [英] How make connection with database SQL server in visual studio

查看:167
本文介绍了如何在visual studio中与数据库SQL服务器建立连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



当我按下submit_btn时,它的打开错误页面告诉我登录失败

我试图打开连接。

任何人都可以帮我解决问题。

我的代码:



  protected   void  submit_btn_Click( object  sender, EventArgs e)
{
string sql = select * from User;
SqlConnection conn = new SqlConnection( 数据Source =。; Initial Catalog = ecommerceDBConnectionString1; Integrated Security = True;);
conn.Open();
SqlCommand command = new SqlCommand(sql,conn);
SqlDataReader dataReader = command.ExecuteReader();

string name = deego;
string password = 123;
while (dataReader.Read())
{
if (name == dataReader.GetString( 1 )。ToString()。Trim())
{
Response.Write( 找到的名称);

if (password == dataReader.GetValue( 2 )。ToString( ).Trim())
{
Response.Write( 我是真的);

}
else
Response.Write( 不正确);


break ;
}

}

}

解决方案

好的,你似乎对大量的东西感到困惑。



首先,永远不要在代码中放置连接字符串。总是把它放在应用程序.config文件中。这是因为当SQL Server名称发生更改时,例如当您从开发机器部署到生产机器时,服务器名称将更改。



接下来,您似乎不理解连接字符串中的参数。对于SQL Server连接字符串,数据源是运行SQL Server的服务器的名称,也可以是SQL Server实例的名称。在你的情况下,。意味着SQL Server应该与您的代码在同一台机器上运行。是这样的吗?可能不是。



接下来,初始目录是您要连接的数据库的名称。您的SQL Server上的数据库名为ecommerceDBConnectionString1。可能不是。



最后,在您的情况下,可信连接是一个选项,告诉连接使用您的代码运行的帐户对SQL Server进行身份验证。在你的开发机器上,代码就像你一样运行。在服务器上,它运行为专门为代码设置的任何帐户,通常是默认的ASP.NET帐户。此帐户很少有有效的SQL Server登录。



对于您来说,连接字符串可能看起来更像这样:

 Server =  serverName ; Database =  databaseName ;用户ID =  sqlServerAccount ;密码= 密码 ; 





但是,确切的语法取决于你正在使用的提供商。



底线是你真的需要在连接字符串是什么以及连接到SQL Server的选项上做你的功课。


请发布错误按下提交按钮以确定问题发生的位置时显示消息。

或者还检查您的/远程计算机上是否正在运行Sql Server服务。


它看起来很明显您的应用程序无法使用Windows身份验证连接到数据库。虽然你正在使用。 (点)应该指向localhost。也许你需要使用正确的连接字符串来连接数据库。



你可以先创建一个数据链接文件(UDL文件)来检查你是否能连接到它的数据库。然后,您可以从那里复制连接字符串。请查看以下步骤来创建UDL文件并使用它。





步骤1:右键单击桌面并选择新建>文本文献。



第2步:将文件名更改为conn.udl(是的,也可以更改文件扩展名)



步骤3:现在您可以检查从普通txt文件更改的文件的图标。双击该文件,您可以看到一个名为数据链接属性的窗口。



第4步:转到第一个标签,我的意思是提供商标签。在那里,我们可以找到许多OLE DB提供程序。选择合适的提供商名称。在您的情况下,您可以选择Microsoft OLE DB提供程序的SQL Server。



步骤5:然后点击下一步>>按钮,将我们带到连接选项卡。我们需要选择或提供服务器名称。在您的情况下,您可以只提供一个。或写入localhost或您的系统本地IP。



步骤6:由于您对Windows身份验证感兴趣,您可以选择使用Windows NT集成安全性


步骤7:然后选择在服务器上选择数据库(默认)。单击下拉列表以查看所有存在的数据库。如果在获取数据库名称时看到任何错误,则连接数据库时会出现问题。更好地提供正确的服务器名称,并使用正确的用户名和密码连接数据库,并从步骤5开始重复。



步骤8:看到数据库后,单击测试连接按钮和时钟确定。



步骤9:现在我们已准备好连接字符串。关闭窗口并右键单击Conn.UDL文件。用记事本打开文件。我们应该有必要的连接字符串。



BTW,保持连接字符串在配置文件中


when i press submit_btn it's open error page tell me the login failed
where i'm trying to open connection .
any one can help me how to fix it .
my code :

protected void submit_btn_Click(object sender, EventArgs e)
    {
        string sql = "select * from User";
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=ecommerceDBConnectionString1;Integrated Security=True;");
        conn.Open();
        SqlCommand command = new SqlCommand(sql, conn);
        SqlDataReader dataReader = command.ExecuteReader();
     
        string name = "deego";
        string password = "123";
        while (dataReader.Read())
        {
            if (name == dataReader.GetString(1).ToString().Trim())
            {
                Response.Write("Name found");

                if (password == dataReader.GetValue(2).ToString().Trim())
                {
                    Response.Write("i's true");

                }
                else
                    Response.Write("Not true");


                break;
            }

        }

   }

解决方案

OK, you seem to be confused about a ton of stuff.

First, NEVER put a connection string inside your code. ALWAYS put it in the applications .config file. This is because WHEN the SQL Server name changes, like when you go from a dev machine and deploy to a production machine, the server name WILL CHANGE.

Next, you don't seem to understand the parameters in a connection string. For an SQL Server connection string, "Data Source" is the name of the server that is running SQL Server and, optionally, the name of an SQL Server instance. In your case, the "." means that the SQL Server is expected to be running on the same machine as your code. Is that the case? Probably not.

Next, the "Initial Catalog" is the name of the database you're connecting to. Is your database on the SQL Server called "ecommerceDBConnectionString1". Probably not.

Lastly, in your case, "Trusted Connection" is an option telling the connection to authenticate to the SQL Server using the account that your code is running under. On your dev machine, the code runs as YOU. On a server, it runs as whatever account is setup specifically to the the code, usually the default ASP.NET account. This account rarely ever has a valid login to an SQL Server.

For you, a connection string would probably look more like this:

Server=serverName;Database=databaseName;User Id=sqlServerAccount;Password=password;



But, the exact syntax depends on what Provider you're using.

Bottom line is you REALLY need to do your homework on what a connection string is and what your options are for connection to an SQL Server.


Please post the error message when you press submit button to identify where the problem occured.
Or also check the Sql Server service is running on your/remote computer.


It clearly seems that you application is not able to connect to the database using the windows authentication. Though you are using . (dot) which should point to the localhost. Perhaps you need to use proper connection string to connect to the database.

You can create a data link file(UDL file) first to check if you able to connect the database from it. Then you can copy the connection string from there. please check below the steps to crate the UDL file and use it.


Step 1: Right click on desktop and select New>Text Document.

Step 2: Change the file name to conn.udl (yes, change the file extension as well)

Step 3: Now you can check the icon for the file changed from normal txt file. Double click on the file and you can see a window named "Data linked properties".

Step 4: Go to the first tab, I mean the provider tab. There we can find number of OLE DB providers. Choose the suitable provider name. At you case you can select "Microsoft OLE DB provider for SQL Server".

Step 5: Then Click "Next>>" button, that will take us to the connection tab. There we need to select or provide a server name. In your case, you can provide just a . or write localhost or your system local IP.

Step 6: As you are interested for the Windows auth, you can select "Use Windows NT Integrated security"

Step 7: Then choose "Select the database on the server" (default). Click on the dropdown to see all the database present. If you see any error while getting the database name, then there is some problem on connecting the database. Better provide the correct server name and use the proper user name and password to connect the database and repeat from Step 5.

Step 8 : Once you see the databases, click "Test connection" button and clock ok.

Step 9: Now we are ready with the connection string. Close the window and right click on the Conn.UDL file. Open the file with notepad. We should having the required connection string.

BTW, keep the connection string inside the configuration file


这篇关于如何在visual studio中与数据库SQL服务器建立连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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