本文介绍了您能帮我在此代码中发现错误,提示连接字符串不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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);
}
}
}
}
推荐答案
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>
这篇关于您能帮我在此代码中发现错误,提示连接字符串不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!