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日

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号