(C#)SQL连接语句中的Integrated Security(使用Windows身份验证时,连必填的部分)

自学的时候,视频里的老师直接把项目打开,就演示,不明所以,于是自己各种Baidu,终于发现连接不上的问题原因了。

文本栏里面输入数据库名称,,记下来,

接下来是设计器代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text=="")
            {
                label1.Text = "请输入名称";
            }
            else
            {
                try
                {//这里开始发现问题关键
                  string constr = "server=DESKTOP-447BADG;Integrated Security=SSPI;database=" + textBox1.Text.Trim();
                    SqlConnection conn = new SqlConnection(constr);
                    conn.Open();
                    if(conn.State==ConnectionState.Open)
                    {
                        label1.Text = "数据库已打开";
                    }
                }
                catch
                {
                    MessageBox.Show("连接数据库失败");
                }
            }
        }
    }

}

server里面的东西是画绿圈的地方:

然后如果你登录的时候是这样:

那就必须有Integrated Security=SSPI;

或者 Integrated Security=true; 同样可以;

 

要是这种方式验证的话:

那  string constr = "server=DESKTOP-447BADG;Integrated Security=SSPI;database=" + textBox1.Text.Trim();

就要改成 string constr = "server=DESKTOP-447BADG;;database=" + textBox1.Text.Trim()+"uid=登录名;psw=密码";

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值