-----------------------------配置文件---------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="sql" connectionString="server=.;database=MySecondDB;uid=sa;pwd=520;"/>
</connectionStrings>
</configuration>
----------------------------Program.cs------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _08登陆
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
-----------------------------Form1-------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _08登陆
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
// 如何表示登陆成功了
string uid = txtUid.Text.Trim();
string pwd = txtPwd.Text;
// 验证
string connStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;
string sql = "select count(*) from loginPrimaryTest where uid= '" + uid + "' and pwd='" + pwd + "'";
int count;
using (SqlConnection conn = new SqlConnection(connStr))
{
// 执行对象
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
conn.Open();
count = (int)cmd.ExecuteScalar();
}
}
if (count > 0)
{
MessageBox.Show("登陆成功");
}
else
{
MessageBox.Show("用户名或密码错误");
}
}
}
}
遇到的问题: 发现添加 using System.Configuration 后 ConfigurationManager 依然无效
解决的方法: 在项目的 Reference 中 添加 System.Configuraton