标题:C# 求教数据库连接问题
取消只看楼主
panxue
Rank: 2
等 级:论坛游民
帖 子:10
专家分:10
注 册:2011-8-30
结帖率:66.67%
已结贴  问题点数:20 回复次数:1 
C# 求教数据库连接问题
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace ReportOrders
{
    class Report
    {
        static void Main(string[] args)
        {
            SqlConnection dataConnection = new SqlConnection();

            try
            {
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                builder.DataSource = ".\\SQLExpress";
               
                builder.InitialCatalog = "Northwind";
                builder.IntegratedSecurity = true;
                dataConnection.ConnectionString = builder.ConnectionString;
                dataConnection.Open();

                Console.Write("Please enter a customer ID (5 characters): ");
                string customerId = Console.ReadLine();

                SqlCommand dataCommand = new SqlCommand();
                dataCommand.Connection = dataConnection;
                = CommandType.Text;
                =
                    "SELECT OrderID, OrderDate, ShippedDate, ShipName, ShipAddress, " +
                    "ShipCity, ShipCountry " +
                    "FROM Orders WHERE CustomerID = @CustomerIdParam";

                SqlParameter param = new SqlParameter("@CustomerIdParam", SqlDbType.Char, 5);
                param.Value = customerId;
                dataCommand.Parameters.Add(param);

                Console.WriteLine("About to find orders for customer {0}\n\n", customerId);
                SqlDataReader dataReader = dataCommand.ExecuteReader();

                while (dataReader.Read())
                {
                    int orderId = dataReader.GetInt32(0);
                    if (dataReader.IsDBNull(2))
                    {
                        Console.WriteLine("Order {0} not yet shipped\n\n", orderId);
                    }
                    else
                    {
                        DateTime orderDate = dataReader.GetDateTime(1);
                        DateTime shipDate = dataReader.GetDateTime(2);
                        string shipName = dataReader.GetString(3);
                        string shipAddress = dataReader.GetString(4);
                        string shipCity = dataReader.GetString(5);
                        string shipCountry = dataReader.GetString(6);
                        Console.WriteLine(
                            "Order: {0}\nPlaced: {1}\nShipped: {2}\n" +
                            "To Address: {3}\n{4}\n{5}\n{6}\n\n", orderId, orderDate,
                            shipDate, shipName, shipAddress, shipCity, shipCountry);
                    }
                }

                dataReader.Close();

            }
            catch (SqlException e)
            {
                Console.WriteLine("Error accessing the database: {0}", e.Message);
            }
            finally
            {
                dataConnection.Close();
            }
        }
    }
}
整个程序运行时的结果:erro accessing the database:无法打开登录所请求的数据库“Northwind”,登录失败。
搜索更多相关主题的帖子: 数据库连接 
2011-09-04 21:53
panxue
Rank: 2
等 级:论坛游民
帖 子:10
专家分:10
注 册:2011-8-30
得分:0 
谢谢各位,解决了,数据库设置问题,跟代码没关系!
2011-10-11 17:29



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-349167-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045780 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved