首页 新闻 会员 周边 捐助

Linq to SQL问题

0
[待解决问题]


其他信息: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)

代码:program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DemoEntity;
namespace DemoExecuteQuery1
{
    class Program
    {
        static void Main(string[] args)
        {
            AddressBookDataContext abc = new AddressBookDataContext();
            //利用ExecuteQuery方法使用SQL命令查询
            var rows = abc.ExecuteQuery<AddressBookEntity>("select * from TabAddressBook where ab_age>{0}", 10);

            foreach (var item in rows)
            {
                Console.WriteLine("{0}的电话为:{1}", item.Name, item.TelephoneNumber);
            }
            Console.ReadKey();

        }
    }
}

AddressBookDataContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;
using System.Data.Linq;

namespace DemoEntity
{
    //使用databaseAttribute映射数据库
    [DatabaseAttribute(Name="DBAddressBook")]
    public class AddressBookDataContext:DataContext
    {
        private const string CONNSTR = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + "\"" + @"C:\Users\DUAN\Documents\Visual Studio 2012\Projects\123\123\123\DBAddressBook,mdf" + "\"" + @";Integrated Security=True;User Instance=True";
        public AddressBookDataContext()
            : base(CONNSTR)
        {
 
        }

        public Table<AddressBookEntity> TabAddressBook { get { return this.GetTable<AddressBookEntity>(); } }
    }
          
}

AddressBookEntity.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq.Mapping;

namespace DemoEntity
{
    //使用TableAttribute映射表格
    [Table(Name="AddressBook")]
    public class AddressBookEntity
    {
        //使用ColumnAttribute映射列
        [Column(AutoSync = AutoSync.OnInsert, DbType = "BigInt NOT NULL IDENTITY", IsDbGenerated = true, Name = "ab_id")]
        public long ID { get; set; }

        [Column(DbType = "varchar(10) NOT NULL", Name = "ab_name", CanBeNull = false)]
        public string Name { get; set; }

        [Column(Name = "ab_dub", DbType = "Varchar(50) NOT NULL")]
        public string Duty { get; set; }

        [Column(Name = "ab_sex", DbType = "Varchar(4) NOT NULL", CanBeNull = false)]
        public string Sex { get; set; }

        [Column(Name = "ab_age", DbType = "TinyInt")]
        public System.Nullable<byte> Age { get; set; }    //代表一个可以指定为Null的值类型,值类型本身不能为空,但                                                             是引用类型本身可空

        [Column(Name = "ab_address", DbType = "Varchar(200)")]
        public string Address { get; set; }

        [Column(Name = "ab_com", DbType = "Varchar(200)")]
        public string Company { get; set; }

        [Column(Name = "ab_tel", DbType = "Text", UpdateCheck = UpdateCheck.Never)]
        public string TelephoneNumber { get; set; }
    }
}

攻城97的主页 攻城97 | 初学一级 | 园豆:166
提问于:2014-11-18 12:54
< >
分享
所有回答(3)
0

这关LINQ啥事了?你不会看看Connection里面的ConnectionString对了吗?

爱编程的大叔 | 园豆:30844 (高人七级) | 2014-11-18 13:14
0

应该是CONNSTR  这个有错误的。

悟行 | 园豆:12559 (专家六级) | 2014-11-18 13:14
0

        private const string CONNSTR = @"Data Source=.\SQLEXPRESS;AttachDbFilename=" + "\"" + @"C:\Users\DUAN\Documents\Visual Studio 2012\Projects\123\123\123\DBAddressBook,mdf" + "\"" + @";Integrated Security=True;User Instance=True

这写法很奇怪 查下数据库实例 这样是否能正常访问

风醉 | 园豆:1197 (小虾三级) | 2014-11-18 13:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册