您能帮我在此代码中发现错误,提示连接字符串不存在 [英] Can you help me find an error in this code it says connectionstring doesnt exist

查看:81
本文介绍了您能帮我在此代码中发现错误,提示连接字符串不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Microsoft.SqlServer.Management.Smo.SqlEnum;
using Microsoft.SqlServer.Management.Adapters;


namespace map_class
{

    public partial class Form1 : Form
    {
        SqlConnection CN = new SqlConnection(ConnectionString);
        private void button1_Click(object sender, EventArgs e)
        {
            

            string query = "Select * from tbl_User account Where (user_id=@id AND user_password=@pwd)";

            CN.Open();
            SqlCommand myCommand = new SqlCommand(query, CN);
            myCommand.Parameters.Add(new SqlParameter("id", SqlDbType.NVarChar)).Value = this.txtusername.Text;
            myCommand.Parameters.Add(new SqlParameter("pwd", SqlDbType.NVarChar)).Value = this.txtpassword.Text;
            SqlDataReader myReader;
            myReader = myCommand.ExecuteReader();
            myReader.Read();
            if (myReader.HasRows)
            {

                CN.Close();
                Adminform mf = new Adminform();
                mf.Show();
                this.Hide();
            }
            else
            {

                MessageBox.Show("Invalid User Name or Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }
        }
    }
}

推荐答案

尝试一下.. :)

try this.. :)

SqlConnection CN = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionName"].ConnectionString);
      private void button1_Click(object sender, EventArgs e)
      {


          string query = "Select * from tbl_User account Where (user_id=@id AND user_password=@pwd)";

          CN.Open();
          SqlCommand myCommand = new SqlCommand(query, CN);
          myCommand.Parameters.Add(new SqlParameter("id", SqlDbType.NVarChar)).Value = this.txtusername.Text;
          myCommand.Parameters.Add(new SqlParameter("pwd", SqlDbType.NVarChar)).Value = this.txtpassword.Text;
          SqlDataReader myReader;
          myReader = myCommand.ExecuteReader();
          myReader.Read();
          if (myReader.HasRows)
          {

              CN.Close();
              Adminform mf = new Adminform();
              mf.Show();
              this.Hide();
          }
          else
          {

              MessageBox.Show("Invalid User Name or Password", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

          }
      }



和您在Web配置中的连接字符串是.. :)



and your connection string in web config is.. :)

<connectionstrings>
   <add name="ConnectionName" providername="System.Data.SqlClient" connectionstring="Data Source=serverName;Initial Catalog=DatabaseName;User ID=User;Password=****" />
 </connectionstrings>


现在它说配置管理器不存在,我必须为此添加一个参考吗?什么是Web配置对C#来说还很新?
it now says configuration manager doesn''t exist do I have to put a reference for it? Whats a web config am still new to c#?


这篇关于您能帮我在此代码中发现错误,提示连接字符串不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆