| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 791 人关注过本帖
标题:C# 求教数据库连接问题
只看楼主 加入收藏
panxue
Rank: 2
等 级:论坛游民
帖 子:10
专家分:10
注 册:2011-8-30
结帖率:66.67%
收藏
已结贴  问题点数:20 回复次数:7 
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
gmpd123
Rank: 1
等 级:新手上路
帖 子:2
专家分:5
注 册:2010-1-24
收藏
得分:5 
确定你的数据库服务开启了,并且确定在.\\SQLExpress下有Northwind数据库
2011-09-04 22:23
Nik2011
Rank: 2
等 级:论坛游民
帖 子:23
专家分:37
注 册:2011-4-28
收藏
得分:5 
回复 楼主 panxue
没有连接字符串吧
2011-09-08 09:20
dotnetcode
Rank: 2
等 级:论坛游民
帖 子:30
专家分:78
注 册:2011-9-8
收藏
得分:5 
没见过这样链接数据库的

[url=http://www.]中国乡卫网[/url]
2011-09-08 14:49
here20082001
Rank: 2
等 级:论坛游民
帖 子:28
专家分:18
注 册:2011-3-21
收藏
得分:5 
写的那么复杂的样子,怎么不简化一点呢
2011-09-08 16:31
lianiamfly
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2011-9-14
收藏
得分:0 
SqlConnection sqlConnection =
程序代码:
 SqlConnection sqlConnection = new SqlConnection("Data Source = liuxueqin; Initial Catalog=MyTest;Integrated Security=True");
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("Select * from RWTest", sqlConnection);
            SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
            DataSet dataSet = new DataSet("RWTest");
            sqlDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;//确定现有 DataSet 架构与传入数据不匹配时需要执行的操作。
2011-09-14 15:19
panxue
Rank: 2
等 级:论坛游民
帖 子:10
专家分:10
注 册:2011-8-30
收藏
得分:0 
谢谢各位,解决了,数据库设置问题,跟代码没关系!
2011-10-11 17:29
C_Guangwei
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-10-10
收藏
得分:0 
如何解决的呢?
2011-10-11 19:56
快速回复:C# 求教数据库连接问题
数据加载中...
 
   



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

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