【问题标题】:How to Connect to SQL Server Using ADO.Net and VB.Net that is Not Linq to SQL and not Entity Framework [duplicate]如何使用不是 Linq to SQL 而不是实体框架的 ADO.Net 和 VB.Net 连接到 SQL Server [重复]
【发布时间】:2015-06-05 19:25:25
【问题描述】:

因此,对于我的问题,我正在为一家使用 SQL 和 Visual Studio 的公司工作,并且我正在接受培训,因为我以前从未使用过 SQL。他们说他们使用 ADO.Net 连接到 SQL 服务器的方式,但是正如我一直在研究的那样,有很多不同的方法可以做到这一点。

我的问题是,在控制台应用程序中,如果我已经有一个连接字符串并且我不需要构建一个,我如何连接到不使用实体框架或使用 LINQ to SQL 的 SQL?

【问题讨论】:

  • 在初始化时使用SqlConnection 对象中的连接字符串。 Doc
  • @OneFineDay 你能举个例子吗?
  • 这取决于您希望如何返回数据。 dataAdapter 填充 DataTable,这对于 DataGridViews 非常有用。有一个 dataReader 可以让您在循环中获取信息。

标签: c# sql-server vb.net connection-string


【解决方案1】:

This page 展示了一个很好的例子来说明如何去做。以下是您最感兴趣的一段代码:

using (SqlConnection con = new SqlConnection(connectionString))
{
    con.Open();

    using (SqlCommand command = new SqlCommand("SELECT TOP 2 * FROM Dogs1", con))
    using (SqlDataReader reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            //Use reader.GetInt32, reader.GetString, etc 
            //depending on the type of data in the table
            Console.WriteLine("{0} {1} {2}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2));
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-01-15
    • 2012-10-03
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 2010-09-22
    相关资源
    最近更新 更多