且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

嘿我连接到我的SQL数据库时遇到问题。我第一次启动我的网站

更新时间:2023-12-02 16:10:22

可能有一些原因:

1)服务中的SQL Server未运行。

2)分布式事务处理协调器服务未运行。

请检查这两项服务并检查连接字符串。

First time i launch my website i get this error

System.ComponentModel.Win32Exception network path was not found



but when i refresh my site there is no problem.

I am using and db data access file written in C#, when i debug it shows there error is in line 22. But i dont understand why, because i am opening and closing everytime.

my dbdataAccess file

public class dbDataAccess
{
    readonly string _strDB = ConfigurationManager.ConnectionStrings["connect"].ConnectionString;

    public DataTable GetData(SqlCommand cmd)
    {
       
        DataSet objDS = new DataSet();
        SqlConnection objConnect = new SqlConnection(_strDB);

            cmd.Connection = objConnect;
            SqlDataAdapter objDa = new SqlDataAdapter();
            objDa.SelectCommand = cmd;
            objDa.Fill(objDS);//this is where it fails

      
            objConnect.Close();
        

        return objDS.Tables[0];
    }   

    public void ModifyData(SqlCommand cmd)
    {
        
        SqlConnection objConnect = new SqlConnection(_strDB);

        cmd.Connection = objConnect;
        objConnect.Open();
        cmd.ExecuteNonQuery();
        objConnect.Close();
    }

    public int intModifyData(SqlCommand CMD)
    {
        SqlConnection objConn = new SqlConnection(_strDB);
        int antalrk;
        try
        {
            CMD.Connection = objConn;
            objConn.Open();
            antalrk = CMD.ExecuteNonQuery();
        }
        catch
        {
            throw;

        }

        finally
        {
            objConn.Close();
        }

        return antalrk;
    }





}



And my connection string in web config

<add name="connect" connectionString="Data Source=**********;Initial Catalog=********;User ID=********;Password=********" providerName="System.Data.SqlClient"/>



and the first method i call.

public DataTable GetAll()
   {
       string strSQL = "********";
       SqlCommand CMD = new SqlCommand(strSQL);
       return objCMD.GetData(CMD);

   }



What I have tried:

debugging and search in google and tryede different solutions and connection strings

There may be some reasons :
1) SQL Server in services is not running.
2) Distributed Transaction Coordinator service is not running.
Please check these two services and check connection string also.