连接到放置在另一个程序中的数据库 [英] connect to database placed in another program

查看:76
本文介绍了连接到放置在另一个程序中的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,我不知道如何解决:(

我在C#中有2个程序

首先是服务器程序
第二个是客户端程序

第一个程序有2个表的数据库

我希望客户端连接到放置在第一个程序中的数据库
我该如何解决?


在此先感谢

代码是

I have problem I don''t know how to solve it :(

I have 2 program in C#

the first is the server program
the second is the client program

the first program have database with 2 table

I want the client to connect to the database placed in the first program
how can I solve that ?


thanks in advance

the code is

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;

namespace PC
{
    class DataBase
    {
        public static Boolean AddTodo(string name, string email, string password)
        {
            Boolean c = false;

            try
            {
                SqlConnection connection = new SqlConnection();
                connection.ConnectionString = @"Data Source=localhost;Initial Catalog=DataBaseWin;Integrated Security=SSPI";
                //Data Source=D:\max\مشروع التخرج\New Code\ServerWin\ServerWin\DataBaseWin.sdf
                connection.Open();

                SqlCommand command1 = new SqlCommand("SELECT * FROM UserInfo.  WHERE UserName= @UserName", connection);
                command1.Parameters.Add(new SqlParameter("@UserName", name));
                command1.ExecuteNonQuery();

                SqlDataReader reader = command1.ExecuteReader();
                {
                    if (!reader.HasRows)
                    {
                        reader.Close();

                        SqlCommand command = new SqlCommand("insert into UserInfo. (UserName, UserEmail, Password) values (@UserName, @UserEmail, @Password)", connection);
                        command.Parameters.Add(new SqlParameter("@UserName", name));
                        command.Parameters.Add(new SqlParameter("@UserEmail", email));
                        command.Parameters.Add(new SqlParameter("@Password", password));

                        command.ExecuteNonQuery();
                        c = true;
                    }
                    else
                    {
                        MessageBox.Show("UserName is thaken");
                    }
                }
                connection.Close();
            }
            catch (SqlException e)
            {
                MessageBox.Show(e.Message);
            }
            return c ;
        }


        public static Boolean Signin(string name, string password)
        {
            Boolean c = false;

            try
            {

                SqlConnection connection = new SqlConnection();
                connection.ConnectionString = @"Data Source=localhost;Initial Catalog=DataBaseWin;Integrated Security=SSPI";
                
                connection.Open();

                SqlCommand command1 = new SqlCommand("SELECT * FROM UserInfo.  WHERE UserName= @UserName AND Password= @Password ", connection);
                command1.Parameters.Add(new SqlParameter("@UserName", name));
                command1.Parameters.Add(new SqlParameter("@Password", password));
                command1.ExecuteNonQuery();

                SqlDataReader reader = command1.ExecuteReader();
                {
                    if (!reader.HasRows)
                    {
                        reader.Close();
                        c = true;
                    }
                    else
                    {
                        MessageBox.Show("Your UserName OR Password Not Right");
                    }
                }
                connection.Close();
            }
            catch (SqlException e)
            {
                MessageBox.Show(e.Message);
            }
            return c;
        }
    }
}


此代码在不存在数据库的第二个程序中
服务器程序中的数据库


错误是
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)


this code in the second program where the database is not there
the database in the server program


the error is
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)

推荐答案

一个开始的地方是: WCF数据服务 [^ ]

然后是 ADO.NET数据服务/RIA服务简介 [ ^ ]萨莎·巴伯(Sacha Barber)

[更新]
尝试将localhost替换为(local)

One place to start is here: WCF Data Services[^]

Then there is Introduction to ADO.NET Data Services/RIA Services[^] By Sacha Barber

[Update]
Try replacing localhost with (local)

Data Source=(local)



最好的问候
Espen Harlinn



Best regards
Espen Harlinn


通常是外围应用配置错误或 sql服务配置问题.请查看此线程的潜在原因和解决方法: ^ ]

如果需要,请在此处参见设置外围应用配置的步骤. .

在这里,请查看以下与之相关的博客条目:
在默认设置下,SQL Server不允许远程连接 [ ^ ]
它说:
I.确保在外围应用配置器"工具中启用了远程连接"
二.检查是否在SQL Server配置管理器中启用了TCP/IP协议
三,确保SQL Server浏览器服务正在运行

还要看这个:
如何将SQL Server 2005配置为允许远程连接 [类似问题:与网络相关. [ SQL服务器修复错误(提供者:命名管道 [
Generally, it is surface area configuration error or sql service configuration issue. Have a look at this thread for potential reasons and resolutions: Resolving A network-related or instance-specific error occurred while establishing a connection to SQL Server[^]

If needed, See steps for setting up Surface area configuration here..

Here, look at the following blog entries related to the same:
Under the default settings SQL Server does not allow remote connections[^]
It says:
I. Make sure that Remote Connections are enabled in Surface Area Configuration tool
II. Check Whether TCP/IP protocol is enabled in the SQL Server Configuration Manager
III. Make sure that SQL server Browser service is running

Also look at this:
SQL SERVER – FIX : ERROR : (provider: Named Pipes Provider, error: 40...[^]
It says:
I. SQL Server should be up and running.
II. Enable TCP/IP in SQL Server Configuration
III. Open Port in Windows Firewall
IV. Enable Remote Connection
V. Enable SQL Server Browser Service
VI. Create exception of sqlbrowser.exe in Firewall
VII. Recreate Alias


If these does not resolve your issue, have a look at these thread:
How to configure SQL Server 2005 to allow remote connections[^]
Similar issues: A network-related..[^]
SQL SERVER FIX ERROR (provider: Named Pipes [^]


//Provide the IP address of the database server in you Data Source instead of 'localhost'  like below
connection.ConnectionString = @"Data Source='192.168.1.127';Initial Catalog=DataBaseWin;Integrated Security=SSPI";


这篇关于连接到放置在另一个程序中的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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