level 1
Is__板
楼主
用C#写一个远程访问数据库里的数据达到通过密码验证的效果,在本地上测试的时候是成功的了,但是我把程序拿到别人电脑的时候出现了Sqlbll.cs类型初始值设定项引发异常
SQL远程访问的相关设置我都配置好了的,这是配置文件的相关代码
<connectionStrings>
<add name="sqlconstring" connectionString="Data Source=14.105.199.47;Initial Catalog=DB201106084107;User ID=sa;Password=qwer1234;Integrated Security=false;persist security info=false;"
providerName="System.Data.SqlClient" />
</connectionStrings>
这是Sqlbll.cs的数据库链接跟密码认证函数
public class SqlBll
{
public static string cnstr = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconstring"].ConnectionString.ToString();
public static SqlConnection cn = new SqlConnection(cnstr);
public static SqlConnection CreateConnect() //创建对远程数据库的连接
{
try
{
string constr = @"Data Source=14.105.199.47;Initial Catalog=DB201106084107;User ID=sa;Password=qwer1234;Persist Security Info=True";
SqlConnection cn = new SqlConnection(constr);
if (cn.State == ConnectionState.Open) { return cn; }
if (cn.State != ConnectionState.Connecting)
{
cn.Open(); //打开连接
return cn; //如果连接打开,返回连接
}
return cn;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "CreateConnect函数", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw ex;
}
}
public static int usrLogin(string userID, string userPW)
{
int n = 0;
string ss = "函数usrLogin()执行错误,密码或名称输入错误:";
try
{
StringBuilder sql = new StringBuilder();
sql.Append("select count(1) from users "); //空格不能省略
sql.Append("where uid=@userID and upw=@userPW");
SqlParameter[] para = { new SqlParameter("@userID",SqlDbType.VarChar,16),
new SqlParameter("@userPW",SqlDbType.VarChar,16)
};
para[0].Value = userID;
para[1].Value = userPW;
n = Convert.ToInt32(SqlDbDAL.ExecScalar(sql.ToString(), CommandType.Text, para));
}
catch (Exception ex)
{
MessageBox.Show(ss + ex.Message, "出错提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
return n;
}
新手一个,因为作业需要用的C#做的,希望朋友们说得通俗点,能提供解决办法更好。
谢谢!
2014年04月07日 15点04分
1
SQL远程访问的相关设置我都配置好了的,这是配置文件的相关代码
<connectionStrings>
<add name="sqlconstring" connectionString="Data Source=14.105.199.47;Initial Catalog=DB201106084107;User ID=sa;Password=qwer1234;Integrated Security=false;persist security info=false;"
providerName="System.Data.SqlClient" />
</connectionStrings>
这是Sqlbll.cs的数据库链接跟密码认证函数
public class SqlBll
{
public static string cnstr = System.Configuration.ConfigurationManager.ConnectionStrings["sqlconstring"].ConnectionString.ToString();
public static SqlConnection cn = new SqlConnection(cnstr);
public static SqlConnection CreateConnect() //创建对远程数据库的连接
{
try
{
string constr = @"Data Source=14.105.199.47;Initial Catalog=DB201106084107;User ID=sa;Password=qwer1234;Persist Security Info=True";
SqlConnection cn = new SqlConnection(constr);
if (cn.State == ConnectionState.Open) { return cn; }
if (cn.State != ConnectionState.Connecting)
{
cn.Open(); //打开连接
return cn; //如果连接打开,返回连接
}
return cn;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "CreateConnect函数", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
throw ex;
}
}
public static int usrLogin(string userID, string userPW)
{
int n = 0;
string ss = "函数usrLogin()执行错误,密码或名称输入错误:";
try
{
StringBuilder sql = new StringBuilder();
sql.Append("select count(1) from users "); //空格不能省略
sql.Append("where uid=@userID and upw=@userPW");
SqlParameter[] para = { new SqlParameter("@userID",SqlDbType.VarChar,16),
new SqlParameter("@userPW",SqlDbType.VarChar,16)
};
para[0].Value = userID;
para[1].Value = userPW;
n = Convert.ToInt32(SqlDbDAL.ExecScalar(sql.ToString(), CommandType.Text, para));
}
catch (Exception ex)
{
MessageBox.Show(ss + ex.Message, "出错提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
return n;
}
新手一个,因为作业需要用的C#做的,希望朋友们说得通俗点,能提供解决办法更好。
谢谢!