The visual studio cannot connect to the server.

Asked 2 years ago, Updated 2 years ago, 121 views

Using Visual studio 2017, I tried to write and verify programming with a simple WebFormApplication to verify connectivity with SQL Server, but the test connection and build went well, but when I actually moved it, I got an "unhandled exception."What is the cause?

using System;
using System.Collections.General;
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 TestConnection 2
{
    public partial class Form 1:Form
    {
        public Form 1()
        {
            InitializeComponent();
        }

        public string contring="Server=localhost\\SQLEXPRESS; Database=master; Trusted_Connection=True";

        private void button1_Click(object sender, EventArgse)
        {
            SqlConnection con=new SqlConnection(conString);
            con.Open();
            if(con.State==System.Data.ConnectionState.Open)
            {
                string q = "insert into User(id, name) values('+textID.Text.ToString()+"', '+textName.Text.ToString()+");
                SqlCommand cmd = new SqlCommand(q,con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Connection made Successful.");
            }
        }
    }
}

c# visual-studio sql-server

2022-09-30 19:37

1 Answers

There was no problem using the same code.Is it a SQLEXPRESS issue?The User table is not known if it is in the master database.


2022-09-30 19:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.