C#crud与sql server [英] C# crud with sql server

查看:99
本文介绍了C#crud与sql server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b $ b

我有以下代码段:



 使用(SqlConnection con =  new  SqlConnection(  Server = USER_PC; Database = CompanyName; User Id =''; Password =''))
{
con.Open();
使用(SqlCommand com = new SqlCommand( SELECT CompanyName FROM CompanyTable,con))
{
using (SqlDataReader reader = com.ExecuteReader())
{
while (reader.Read())
{
string strCompanyName = ;

Console.WriteLine( 公司名称:{0} \ n {1} ,strCompanyName);
}
}
}
}





我正在使用sql server 2008管理工作室



当我跑步时我收到此错误:



 A网络相关的特定于实例的错误发​​生,而建立连接 SQL Server。服务器找到 无障碍。验证实例名称 正确 SQL Server 配置 允许远程连接。 (提供商:命名管道提供商,错误: 40   - 可以打开连接 to  SQL Server)

绿色箭头指向 to this:con.Open();

我缺少什么?

知道如果我必须将放到 中,以便更轻松。

谢谢

解决方案

正如所建议的那样,这是你的连接字符串。

尝试使用服务器资源管理器窗格在VS中设置连接:

1)打开服务器资源管理器。

2)右键单击数据连接并选择添加连接

3)在随后的对话框中,选择您的DataSource和数据库,指定安全信息,然后按测试连接按钮。

4)连接时工作,按确定

5)在服务器资源管理器窗格中突出显示数据库,然后查看属性窗格。将显示连接字符串的工作示例,您可以将其复制并粘贴到您的应用程序或配置文件中。



BTW:硬连线不是一个好主意连接字符串 - 它们可能会发生变化,特别是当您转移到生产环境时。尝试将字符串放入配置文件或类似文件中,无需重新编译应用程序即可对其进行更改!


如果使用SQL身份验证,则用户ID不能为空

此错误取决于您使用的实例名称。此处标记了所谓的代码(Server = USER_PC ; Database = CompanyName; User Id =''; Password =' ')。尝试使用 USER_PC\SQLEXPRESS



问候......:笑:


Hi
I have the following code segment :

using (SqlConnection con = new SqlConnection("Server=USER_PC;Database=CompanyName;User Id='';Password=''"))
            {
                con.Open();
                using (SqlCommand com = new SqlCommand("SELECT CompanyName FROM CompanyTable", con))
                {
                    using (SqlDataReader reader = com.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string strCompanyName = "";

                            Console.WriteLine("Company Name: {0}\n    {1}", strCompanyName);
                        }
                    }
                }
            }



I am using sql server 2008 Management studio

When I run I get this error :

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

And the green arrow points to this   : con.Open();

What am I missing ?

Please let me know if I must put screen shots of the problem to make it easier.

Thanks

解决方案

As has been suggested, it's your connection string.
Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app or config file.

BTW: It's not a good idea to "hardwire" connections strings - they are liable to change, particularly when you move to a production environment. Try putting the string into a config file or similar, where it can be changed without needing a recompile of your application!


if you are using SQL authentication , User ID cannot be blank


This error depends on your instance name that is used.Suspected code is marked here ("Server=USER_PC;Database=CompanyName;User Id='';Password=''").Try using USER_PC\SQLEXPRESS.

Regards.. :laugh:


这篇关于C#crud与sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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