Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Visual C#连接到远程SQL Server数据库?_C#_Sql_Ado.net - Fatal编程技术网

如何使用Visual C#连接到远程SQL Server数据库?

如何使用Visual C#连接到远程SQL Server数据库?,c#,sql,ado.net,C#,Sql,Ado.net,我正在创建Windows窗体应用程序,我的SQL Server数据库位于远程服务器上。如何使用Visual C#和ADO.NET连接到它?答案可以在这里找到- 此外,请在此阅读并下载- 您需要调查.NET中的SqlConnection、SqlCommand以及可能的SqlDataReader和SqlDataAdapter组件(请参阅MSDN在线文档) 一旦你有了它,你需要定义你的-检查该站点链接,以获得大量的选择和连接字符串的解释 然后,您基本上使用以下方式进行连接: using(SqlCon

我正在创建Windows窗体应用程序,我的SQL Server数据库位于远程服务器上。如何使用Visual C#和ADO.NET连接到它?

答案可以在这里找到-

此外,请在此阅读并下载-
您需要调查.NET中的
SqlConnection
SqlCommand
以及可能的
SqlDataReader
SqlDataAdapter
组件(请参阅MSDN在线文档)

一旦你有了它,你需要定义你的-检查该站点链接,以获得大量的选择和连接字符串的解释

然后,您基本上使用以下方式进行连接:

using(SqlConnection conn = new SqlConnection('your connection string here'))
{
    conn.Open();
    // do stuff
    conn.Close();
}
你可以用不同的方式做一些事情,比如通过填充数据集,读取值等等


阅读MSDN开始!或者谷歌的“ADO.NET教程”-你会发现大量的链接。

在MS SQL Server的眼中,SQL Server所在的位置没有区别。您所需要的只是确保您可以通过IP和端口号访问该服务器。

使用以下代码创建所需的连接对象

public bool BeginTransaction(string strServerName) {
    try
    {
        bool bRet = OpenConnection(strServerName);
        if (bRet)
        {
            m_objTransaction = m_conn.BeginTransaction();
            m_dtAdapter.SelectCommand.Connection = m_conn;
            return true;
        }
    }
    catch (Exception ex)
    {
        return false;
    }
    return false; 
}

public bool OpenConnection(string strServerName) {
    try
    {
        m_connStr = string.Empty;
        m_connStr = string.Format("Data Source=;Initial Catalog=;User Id=sa;Password=;"); //write your credentials here with DB name and server
        m_conn = new SqlConnection(m_connStr);
        m_conn.Open();

        m_dtAdapter = new SqlDataAdapter();

        if (m_conn != null)
        {
            m_dtAdapter.SelectCommand = new SqlCommand();
        }
    }
    catch (SqlException ex)
    {
        return false;
    }
    catch (Exception ex)
    {
        return false;
    }
    return true; 
}

我的数据库是MSSQL而不是MySQL。为什么-1我可以知道吗?我的答案和挑选的答案一样!!