【问题标题】:ConnectionString in Visual StudioVisual Studio 中的连接字符串
【发布时间】:2012-10-12 04:19:52
【问题描述】:

我正在尝试做一个返回数据集的方法,但连接字符串不起作用。

我试过这个:

    public DataSet list()
    {
        // First Create a New Connection
        System.Data.SqlClient.SqlConnection sqlConnection1 = new System.Data.SqlClient.SqlConnection();
        // Now Pass a Connection String To the Connection


        sqlConnection1.ConnectionString = "Data Source=mysite.com.br;User ID=admin_devr;Password=123456;Initial Catalog="rave_dbteste" providerName="System.Data.OleDb";
        // Now the Select statement you want to run
        string select = "select * from table";
        // Create an Adapter
        SqlDataAdapter da = new SqlDataAdapter(select, sqlConnection1);
        // Create a New DataSet
        DataSet ds = new DataSet();
        // Fill The DataSet With the Contents of the Stock Table
        da.Fill(ds, "cdestacionamento");
        // Now Return ds which is a DataSet
        return (ds);
    }

怎么了?

【问题讨论】:

  • 它给出了什么错误?另外,我认为您缺少关闭“(引号)。并且始终将连接字符串放在 web.config 或 app.config 中。
  • 一个很好的链接:connectionstrings.com/sql-server-2012
  • @bittech 我已关注但不起作用...在连接字符串行中返回错误
  • 实际错误是什么?构建应用程序或运行应用程序时是否出现错误?

标签: visual-studio web-services connection-string


【解决方案1】:

我认为你在下面的连接字符串行之后没有打开连接

sqlConnection1.Open();

并且总是这样检查

 if (sqlConnection1.State == ConnectionState.Closed)
                    sqlConnection1.Open();

【讨论】:

  • 或在这里发布您的错误,阅读错误专家可以帮助您
  • System.Data.SqlClient.SqlException:无法打开登录“rave_dbteste providerName=System.Data.OleDb”中请求的数据库。登录失败。用户“admin_devr”登录失败。
  • 尝试更改您的提供商:providerName="System.Data.SqlClient" 或重新检查您的登录凭据。
  • 如果您有 sql server 尝试使用连接字符串中的这些凭据登录,则您尝试连接的 sql server 没有响应或者用户不存在
  • 现在,我收到以下错误:SELECT permission denied on object 'table'、database 'rave_dbteste'、owner 'admin_devr'。
猜你喜欢
  • 2014-09-17
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
  • 2015-12-07
  • 1970-01-01
  • 2016-02-23
  • 1970-01-01
相关资源
最近更新 更多