繁体   English   中英

ADO.net如何开始

[英]ADO.net how to start

在过去的3个小时中,我一直在尝试弄清ADO.NET的工作原理是否成功。 有人可以指出我的精彩教程或类似内容吗? 我试图从头开始构建数据库,并在WPF程序中使用它。

我之前使用过JDBC和sqlite,但是没有找到从零到可以连接和查询的数据库的教程。

谢谢你的帮助。

我仍然需要一个很好的例子,从零开始构建数据库。 Northwind的示例对我不起作用。 这是我的代码以及出现的错误:

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);
        }
    }
}

错误:

抛出SqlException
数= 2
消息=建立与SQL Server的连接时发生与网络相关或特定于实例的错误。 服务器未找到或无法访问。 验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

ADO.Net位居中间,所以我建议您从此开始。 我建议先设计数据库。 然后看看这个文章的简单教程如何绑定列表控件在DB的一些数据。

或者,如果您不想使用数据绑定,则可以使用SqlCommand等手动填充GUI。

这里是另一个,更全面的文章,数据在WPF结合。

有关ADO.NET的信息,请检查MSDN 通常,您需要具有要连接的数据库的DB服务器,而不是System.Data.SqlClient命名空间中的三个类:

简单访问的示例是:

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));
    }
  }
}

问题出在您的连接字符串上。 我建议您更改此:

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

对此:

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

如果您需要查找连接字符串,请访问http://www.ConnectionStrings.com 这是一个很棒的网站。

ps最佳实践是将连接字符串存储在app.config或web.config中。

暂无
暂无

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

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