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

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

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

文本栏里面输入数据库名称,(C#)SQL连接语句中的Integrated Security(使用Windows身份验证时,连必填的部分),记下来,

接下来是设计器代码:

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里面的东西是画绿圈的地方:(C#)SQL连接语句中的Integrated Security(使用Windows身份验证时,连必填的部分)

然后如果你登录的时候是这样:(C#)SQL连接语句中的Integrated Security(使用Windows身份验证时,连必填的部分)

那就必须有Integrated Security=SSPI;

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


要是这种方式验证的话:(C#)SQL连接语句中的Integrated Security(使用Windows身份验证时,连必填的部分)

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

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