不使用链接字符串链接数据库,何解?

V0lD 2009-12-25 11:04:46
如何不使用链接字符串链接数据库:(这里是登陆界面里的相关代码)
如下代码:
namespace friendship
{
#region 数据库连接类
class Fconnection
{

public SqlConnection SQlconnection()//数据库连接
{
SqlConnection con = new SqlConnection();
Properties.Settings s = Properties.Settings.Default;
con.ConnectionString = s.sqlconnection;
return con;
}
}
#endregion
}


namespace friendship
{
class use
{
private SqlConnection cn = null;
public SqlDataReader dr = null;

#region//登录信息
public int load(userload user)
{
int intfal = 0;

try
{
Fconnection fcn = new Fconnection();
cn = fcn.SQlconnection();
cn.Open();
//str = "select userName,userPassword from user where userName='" + user.User + "'and userpasswoed'" + user.Pwd + "'";
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.Parameters.Add("@userName", SqlDbType.Char);
cmd.Parameters["@userName"].Value = user.User;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "P_load";

dr = cmd.ExecuteReader();
dr.Read();
if (dr.HasRows)
{
intfal = 1;
}
return intfal;
}
catch (Exception ex)
{
return 0;
}

#endregion
}
}
}


在登陆界面的"登陆"按钮中调用这两个类,通过判断use类返回的值,来确定是否链接到数据库,从而进入登陆界面后的操作的.

请问,为何?即没用到链接字符串就能链接数据库了.
...全文
48 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
V0lD 2009-12-25
  • 打赏
  • 举报
回复
晓得了,原来在config里
hhc123 2009-12-25
  • 打赏
  • 举报
回复
 public SqlConnection SQlconnection()//数据库连接 
{
SqlConnection con = new SqlConnection();
Properties.Settings s = Properties.Settings.Default; //这个啊
con.ConnectionString = s.sqlconnection; //.........
return con;
}

............
wanghui0380 2009-12-25
  • 打赏
  • 举报
回复
谁说没有用呢
Properties.Settings s = Properties.Settings.Default;
s.sqlconnection

这个就是
孟子E章 2009-12-25
  • 打赏
  • 举报
回复
你打印出s.sqlconnection就知道是啥了
孟子E章 2009-12-25
  • 打赏
  • 举报
回复
那是因为你的字符串在config 里面写好了
angel6709 2009-12-25
  • 打赏
  • 举报
回复
SqlConnection con = new SqlConnection();
Properties.Settings s = Properties.Settings.Default;
con.ConnectionString = s.sqlconnection;

110,565

社区成员

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

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

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