简体   繁体   English

ADO.net如何开始

[英]ADO.net how to start

For the last 3 hours I've been trying to figure out how ADO.NET works with no success. 在过去的3个小时中,我一直在尝试弄清ADO.NET的工作原理是否成功。 Could someone point me at a great tutorial or somethign similar? 有人可以指出我的精彩教程或类似内容吗? I am trying to build a DB from scratch and working with it in my WPF program. 我试图从头开始构建数据库,并在WPF程序中使用它。

I have worked before with JDBC and sqlite but I didn't find a tutorial from zero to a DB where I can connect and query. 我之前使用过JDBC和sqlite,但是没有找到从零到可以连接和查询的数据库的教程。

Thanks for your help. 谢谢你的帮助。

I still need one good example with building DB from zero. 我仍然需要一个很好的例子,从零开始构建数据库。 The Northwind example just doesn't work for me. Northwind的示例对我不起作用。 Here's my code and the error I'm getting: 这是我的代码以及出现的错误:

try
        {
            // step 1: create a SqlConnection object to connect to the
            // SQL Server Northwind database
            SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");
            // step 2: create a SqlCommand object
            SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
            // step 3: set the CommandText property of the SqlCommand object to
            // a SQL SELECT statement that retrieves a row from the Customers table
            mySqlCommand.CommandText =
            "SELECT CustomerID, CompanyName, ContactName, Address " +
            "FROM Customers " +
            "WHERE CustomerID = ‘ALFKI’";
            // step 4: open the database connection using the
            // Open() method of the SqlConnection object
            mySqlConnection.Open();
            //DEVELOPING YOUR FIRST ADO.NET PROGRAM 5
            // step 5: create a SqlDataReader object and call the ExecuteReader()
            // method of the SqlCommand object to run the SELECT statement
            SqlDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
            // step 6: read the row from the SqlDataReader object using
            // the Read() method
            mySqlDataReader.Read();
            // step 7: display the column values
            Console.WriteLine("mySqlDataReader[\"CustomerID\"] = " +
            mySqlDataReader["CustomerID"]);
            Console.WriteLine("mySqlDataReader[\"CompanyName\"] = " +
            mySqlDataReader["CompanyName"]);
            Console.WriteLine("mySqlDataReader[\"ContactName\"] = " +
            mySqlDataReader["ContactName"]);
            Console.WriteLine("mySqlDataReader[\"Address\"] = " +
            mySqlDataReader["Address"]);
            // step 8: close the SqlDataReader object using the Close() method
            mySqlDataReader.Close();
            // step 9: close the SqlConnection object using the Close() method
            mySqlConnection.Close();
        }
        catch (SqlException e)
        {
            Console.WriteLine("A SqlException was thrown");
            Console.WriteLine("Number = " + e.Number);
            Console.WriteLine("Message = " + e.Message);
            Console.WriteLine("StackTrace:\n" + e.StackTrace);
        }
    }
}

The error: 错误:

A SqlException was thrown 抛出SqlException
Number = 2 数= 2
Message = A network-related or instance-specific error occurred while establishing a connection to SQL Server. 消息=建立与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. 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

The ADO.Net bit goes in the middle so I'd suggest that that's not what you should start with. ADO.Net位居中间,所以我建议您从此开始。 I'd suggest first designing the DB. 我建议先设计数据库。 Then look at this article for a simple tutorial for how to bind a list control to some data in the DB. 然后看看这个文章的简单教程如何绑定列表控件在DB的一些数据。

Or you could just use SqlCommand etc to populate your GUI manually if you don't want to use data binding. 或者,如果您不想使用数据绑定,则可以使用SqlCommand等手动填充GUI。

And here 's another, more comprehensive article, for data binding in WPF. 这里是另一个,更全面的文章,数据在WPF结合。

For information about ADO.NET check MSDN . 有关ADO.NET的信息,请检查MSDN Generally you need DB server with database you want to connect and than three classes from System.Data.SqlClient namespace: 通常,您需要具有要连接的数据库的DB服务器,而不是System.Data.SqlClient命名空间中的三个类:

Example of simple access is: 简单访问的示例是:

string connectionString = "Data Source=.;Initial Catalog=TestDB;Integrated Security=SSPI";
string query = "SELECT * FROM dbo.TestTable"

using (var connection = new SqlConnection(connectionString))
{
  var command = new SqlCommand(query, connection);
  connection.Open();

  using (var reader = command.ExcecuteReader())
  {
    while (reader.Read())
    {
      Console.WriteLine(reader.GetString(0));
    }
  }
}

The problem is with your connection string. 问题出在您的连接字符串上。 I suggest you change this: 我建议您更改此:

        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");

to this: 对此:

        SqlConnection mySqlConnection = new SqlConnection("server=.;database=Northwind;uid=sa;pwd=sa");

Then go to http://www.ConnectionStrings.com if you ever need to look up connection strings. 如果您需要查找连接字符串,请访问http://www.ConnectionStrings.com It is a great site. 这是一个很棒的网站。

ps Best practice is to store your connection string in app.config or web.config. ps最佳实践是将连接字符串存储在app.config或web.config中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM