ADO.net如何启动
在过去的 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 的连接)
For the last 3 hours I've been trying to figure out how ADO.NET works with no success. 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.
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.
Thanks for your help.
I still need one good example with building DB from zero. The Northwind example just doesn't work for me. 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
Number = 2
Message = A network-related or instance-specific error occurred while establishing a connection to 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. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ADO.Net 位位于中间,因此我建议您不应该从它开始。我建议首先设计数据库。然后查看这篇文章,了解如何将列表控件绑定到数据库中的某些数据。
或者您可以使用 SqlCommand 等如果您不想使用数据绑定,请手动填充 GUI。
这里是另一篇更全面的文章,介绍 WPF 中的数据绑定。
The ADO.Net bit goes in the middle so I'd suggest that that's not what you should start with. 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.
Or you could just use SqlCommand etc to populate your GUI manually if you don't want to use data binding.
And here's another, more comprehensive article, for data binding in WPF.
有关 ADO.NET 的信息,请查看 MSDN。通常,您需要带有要连接的数据库的数据库服务器以及 System.Data.SqlClient 命名空间中的三个类:
简单访问的示例是:
For information about ADO.NET check MSDN. Generally you need DB server with database you want to connect and than three classes from System.Data.SqlClient namespace:
Example of simple access is:
问题出在你的连接字符串上。我建议您将其更改
为:
然后转到 http://www.ConnectionStrings.com 如果您曾经需要查找连接字符串。这是一个很棒的网站。
ps 最佳实践是将连接字符串存储在 app.config 或 web.config 中。
The problem is with your connection string. I suggest you change this:
to this:
Then go to http://www.ConnectionStrings.com if you ever need to look up connection strings. It is a great site.
p.s. Best practice is to store your connection string in app.config or web.config.