using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
namespace 数据库管理系统
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
this.StartPosition = FormStartPosition.CenterScreen;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = " ";
textBox2.Text = " ";
}
private void button1_Click(object sender, EventArgs e)
{
string userName = textBox1.Text;
string password = textBox2.Text;
string connString = ConfigurationManager.ConnectionStrings["LocalDBString"].ConnectionString;
Timeout=30;Encrypt=False;TrustServerCertificate=False";
SqlConnection conn = new SqlConnection(connString);
表示:string”不包含"connectionstring"的定义,并且找不到可接受类型的"string"的第一个扩展方法""connectionstring"
这个 怎么解决啊
string connString = ConfigurationManager.ConnectionStrings["LocalDBString"].ConnectionString;
Timeout=30;Encrypt=False;TrustServerCertificate=False";
这下面一句是什么?
string userName = textBox1.Text;
string password = textBox2.Text;
string connString =ConfigurationManager.ConfigurationManager.ConnectionStrings["LocalDBString"].ConnectionString;
// string connString = @"Data Source=(localdb)\Projects;Initial Catalog="火车票务 系统";Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";
// "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\Users\Administrator.ZX-201609111454\Desktop\期末文档\大二上\C#\MySchool";
SqlConnection conn = new SqlConnection(connString);
string sql =String.Format("select count(*) from [User] where UserName='{0}'and password='{1}'",userName,password);
try
{
conn.Open();// 打开数据库连接
SqlCommand comm = new SqlCommand(sql, conn); //创建 Command 对象
int n = (int)comm.ExecuteScalar();//执行查询语句,返回匹配的行数
if (n == 1)
{
this.DialogResult = DialogResult.OK;
this.Tag = true;
}
else
{
MessageBox.Show("您输入的用户名或密码错误!请重试", "登录失败",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.Tag = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.Tag = false;
}
finally
{
conn.Close(); // 关闭数据库连接
}
我给你看看完整的。。
@一场空:
string connString =ConfigurationManager.ConfigurationManager.ConnectionStrings["LocalDBString"].ConnectionString;
// string connString = @"Data Source=(localdb)\Projects;Initial Catalog="火车票务 系统";Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";
// "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=\"C:\Users\Administrator.ZX-201609111454\Desktop\期末文档\大二上\C#\MySchool";
你这里面几个换行是硬回车还是自动换行?
ConfigurationManager.ConfigurationManager.ConnectionStrings?typo?
你要么贴个代码的图吧
要看你配置文件里面LocalDBString 写在哪了
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
<connectionStrings>
<add name="LocalDBString" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\Administrator.ZX-201609111454\AppData\Local\Microsoft\VisualStudio\SSDT\火车票务 系统.mdf';Integrated Security=True;Connect Timeout=30"/>
</connectionStrings>
</configuration>
这样可以么?
@一场空: 这个应该不是编译后生成的config文件配置吧
去bin目录看看下 还是这样的配置么,如果是还报错。那我也不知道了。这样看代码是正常的。
实在不行换成在 appSetting 里面写吧
@Fan丶: 不懂 。。。
配置文件配置文件
? 说清楚点啊 大哥