heiziling 2021-10-16 14:36 采纳率: 83.3%
浏览 35
已结题

C#与Microsoft SQL Serve的问题,这个是真的不会

我用Windows身份验证登录的Microsoft SQL Server 用的是计算机名加\SQLEXPRESS

我在C#也连接上了

img

运行时输入账号和密码就应该出现

img

可是我的会一直卡在输入账号与密码的界面

img

过一会就会报错

img

这个是代码,帮我看一下我该怎么改

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;

namespace 学生考勤系统
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //获取链接
       public SqlConnection GetCon()
        {
            SqlConnection con = new SqlConnection();
           con.ConnectionString = "Server=.;Trusted_Connection=SSPI;DataBase=xskqDB";
            return con;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string name = this.textBox1.Text.Trim();
            string pwd = this.textBox2.Text.Trim();
            string sqlstr = "select * from suser where name='" + name + "' and password= '" + pwd + "'";

            SqlConnection con = GetCon();

            con.Open();
            
            
            SqlDataAdapter sad = new SqlDataAdapter(sqlstr, con);
            DataTable dt1 = new DataTable();
            sad.Fill(dt1);
            if(dt1.Rows.Count>0)
            {
                //登录主窗体
                this.Hide();
                mainForm mf = new mainForm();
                mf.Show();

            }
            else
            {
                //提示错误,清空输入框
                MessageBox.Show("用户名或密码错误,是否重新输入");
                this.textBox1.Text = "";
                this.textBox2.Text = "";
            }
            con.Close();
        }
    }
}

  • 写回答

1条回答 默认 最新

  • CSDN专家-showbo 2021-10-16 14:49
    关注
    con.ConnectionString = @"Server=.\SQLEXPRESS;Trusted_Connection=SSPI;DataBase=xskqDB";
    
    

    server改这样试试

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月24日
  • 已采纳回答 10月16日
  • 创建了问题 10月16日