C# 连接 到SQL

cdmsan 2008-07-08 12:30:48
出错提示:“System.Data.SqlClient.SqlConnection”的类型初始值设定项引发异常。

连接代码:


using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace SqlDataConfig
{
public class DateLink
{
public SqlConnection myConn;
public string ServerStr;
static string server; //服务器地址
static string uid; //用户名
static string pwd; //密码
static string database; //数据库名称

public static void setServer(string get_server)
{
server = get_server;
}

public static void setUid(string get_uid)
{
uid = get_uid;
}

public static void setPwd(string get_pwd)
{
pwd = get_pwd;
}

public static void setDatabase(string get_database)
{
database = get_database;
}
public DateLink()
{
ServerStr = @"Server=" + server + ";Database=" + database + ";User=" + uid + ";PWD=" + pwd;
myConn = new SqlConnection(ServerStr);//出错提示行:“System.Data.SqlClient.SqlConnection”的类型初始值设定项引发异常。


}
public void Open()
{
myConn.Open();
}
public void Close()
{
myConn.Close();
}
public int tOpen()
{
try
{
myConn.Open();
return 1;
}
catch
{
return 0;
}
}

}

}


调用代码:

DateLink.setServer("127.0.0.1");
DateLink.setUid("sa");
DateLink.setPwd("");
DateLink.setDatabase("AssetManagement");
DateLink MyDL = new DateLink();
...全文
96 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
cdmsan 2008-07-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 tmxk2002 的回复:]
User改为UID,我记得是的,PWD改为Password
[/Quote]

也是相同的错误
cdmsan 2008-07-08
  • 打赏
  • 举报
回复
我是XP系统,SQL2000
cdmsan 2008-07-08
  • 打赏
  • 举报
回复
那是什么原因呀~~~
  • 打赏
  • 举报
回复
User改为UID,我记得是的,PWD改为Password
fsy123456accp 2008-07-08
  • 打赏
  • 举报
回复
郁闷啊 我调了你的代码没有错误啊
beancurd005 2008-07-08
  • 打赏
  • 举报
回复
APP.config建立后,它自带的有个连接字符串,而且连接的是Express SQL 2005,是隐藏的,你在APP.config里面是看不到的

只有读取的时候它才会出来的。
cdmsan 2008-07-08
  • 打赏
  • 举报
回复
唉~~~终于知道是怎么回事了~
多建立了个APP.config文件.将它删除了就可以了~

但是为什么有了APP.config就不可以呢?
cdmsan 2008-07-08
  • 打赏
  • 举报
回复
天呀,什么我就测试有问题的呀~~~
我是VS2005,SQL2000,XP,SP2
bbbbbb888888 2008-07-08
  • 打赏
  • 举报
回复
还是connstr的问题
wzhy_521 2008-07-08
  • 打赏
  • 举报
回复
奇怪了,刚才我也试了一下那个代码,是通的!没有出错!
wzhy_521 2008-07-08
  • 打赏
  • 举报
回复
ServerStr = Data Source=" + server + ";Initial Catalog=" + database + ";Persist Security Info=True;User ID=" + uid + ";Password=" + pwd;
这样试一下!
JTFLSH 2008-07-08
  • 打赏
  • 举报
回复
你换种方式写呢:
ServerStr = "Server='" + server + "';Database='" + database + "';User='" + uid + "';PWD='" + pwd+"'";
看行不行

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧