日期:2014-05-18  浏览次数:20722 次

刚刚接触C#就遇到一系列的问题!求解释
string conn = ConfigurationSettings.AppSettings.Get("connecionString");
SqlCommand cmd = new SqlCommand("select count(*) from tb_Admin where AdminUser='" + textBox1.Text + "' and Adminpassword='" + textBox2.Text + "'",conn);
int i = Convert.ToInt32(cmd.ExecuteScalar());
{
。。。
}
错误提示是:参数2无法转换成string类型
  最匹配的重载方法具有一些无效参数!
我不会改,求指教

------解决方案--------------------
connecionString

没有拼写错误吗?
还是
connectionString

这个节点存在吗
配置文件里面appSettings节点里面有这个节点吗
------解决方案--------------------
C# code

SqlConnection conn=new Sqlconnection();
conn.ConnectionString = ConfigurationSettings.AppSettings.Get("connecionString");
SqlCommand cmd......

------解决方案--------------------
C# code
#region 连接字符串
        const string CONNSTRING = "server=.;database=Library;Trusted_connection=true";
        static SqlConnection conn = new SqlConnection(CONNSTRING);
        public static SqlConnection Conn
        {
          get { return DBsql.conn; }
        }
        #endregion

        #region 打开连接
        public static void Open()
        {
            if (conn.State == ConnectionState.Closed)
            {
                conn.Open();
            }
            else if(conn.State== ConnectionState.Broken)
            {
                conn.Close();
                conn.Open();
            }
        }
        #endregion

        #region 关闭连接
        public static void Close()
        {
            if (conn.State == ConnectionState.Closed || conn.State == ConnectionState.Broken)
            {
                conn.Close();
            }
        }
        #endregion
public static void Ti(string sql,ref SqlDataReader reader)
        {
            SqlCommand comm = new SqlCommand(sql, conn);
            Open();
            reader = comm.ExecuteReader(CommandBehavior.CloseConnection);
        }