问题描述
这是我的第一次,我在Internet上进行了检查,我遵循了每个步骤,但是在sqlConnection
中遇到了一个问题,我认为它说它找不到sql Server,也不知道为什么。 / p>
我正在尝试制作一个简单的雇员数据(带有照片和日期),我想将其保存到sql Server。首先,我创建了一个新的sql Server,并将(localdb)\MSsqlocalDB
作为服务器名称,并将EmployeeDatabase
“作为新的数据库名称。
"Data Source=(localdb)\MSsqlLocalDB;Initial Catalog=USER_TABLE;Integrated Security=True;Pooling=False"
使用此连接字符串时出现此错误:
System.Data.sqlClient.sqlException:'与sql Server建立连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将sql Server配置为允许远程连接。 (提供者:命名管道提供者,错误:40-无法打开与sql Server的连接)'
代码:
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 EmployeeDataSystem
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Insert_button_Click(object sender,EventArgs e)
{
sqlConnection con = new sqlConnection("Data Source=.;Initial Catalog=USER_TABLE;Integrated Security=True;Pooling=False");
con.open();
sqlCommand cmd = new sqlCommand("INSERT INTO Personnel(Id,EmployeeName,EmployeeBirthdate,EmployeeSalary,EmployeeAddress,EmployeeMobile,EmployeeHiredate,EmployeePhoto) VALUES (@Id,@EmployeeName,@EmployeeBirthdate,@EmployeeSalary,@EmployeeAddress,@EmployeeMobile,@EmployeeHiredate,@EmployeePhoto)",con);
cmd.Parameters.AddWithValue("@Id",int.Parse(Id_number.Text));
cmd.Parameters.AddWithValue("@EmployeeName",int.Parse(Employee_name.Text));
cmd.Parameters.AddWithValue("@EmployeeBirthdate",dateTimePicker1.Value);
cmd.Parameters.AddWithValue("@EmployeeSalary",int.Parse(Salary_textBox.Text));
cmd.Parameters.AddWithValue("@EmployeeAddress",int.Parse(Address_textBox.Text));
cmd.Parameters.AddWithValue("@EmployeeMobile",int.Parse(Mobile_textBox.Text));
cmd.Parameters.AddWithValue("@EmployeeHiredate",dateTimePicker2.Value);
cmd.Parameters.AddWithValue("@EmployeePhoto",pictureBox1.Image);
cmd.ExecuteNonQuery();
con.Close();
Id_number.Text = "";
Employee_name.Text = "";
//dateTimePicker1.Value = "";
Salary_textBox.Text = "";
Address_textBox.Text = "";
Mobile_textBox.Text = "";
//dateTimePicker2.Value = "";
//pictureBox1.Image = "";
MessageBox.Show("Successfullly Inserted!");
}
private void pictureBox1_Click(object sender,EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
PictureBox p = sender as PictureBox;
if(p != null)
{
open.Filter = "(*.jpg;*.jpeg;*.bmp;)|*.jpg; *.jpeg; *.bmp;";
if(open.ShowDialog() == DialogResult.OK)
{
p.Image = Image.FromFile(open.FileName);
}
}
}
}
}
解决方法
如果使用默认instst sql服务器,则使用dot(。)作为数据源,否则使用instansname,您可以通过sql server中的服务器名称找到该instansname
SqlConnection con = new SqlConnection("Data Source=your instance name;Initial Catalog=databasename;User Id=your user;Password=your password;Integrated Security=True;Pooling=False");
找到您的实例名称 并选择远程登录到sql server的登录名 然后点击您的用户并设置用户
,初始目录= EmployeeDatabase
如果不确定实例名称(数据源),请执行以下操作:
- 打开命令提示符窗口
- services.msc
- 找到以SQL开头的条目。 每个名为SQL Server(实例名称)的实例都有一个条目。实例名称出现在括号内。
您的图片中的连接字符串为
"Data Source=.;Initial Catalog=USER_TABLE;Integrated Security=True;Pooling=False"
将其更改为
"Data Source=.\MSSQLLocalDB;Initial Catalog=USER_TABLE;Integrated Security=True;Pooling=False"
,
此错误消息的原因很多,如下所示
防病毒, 防火墙功能 SQL Server网络配置 请检查连接字符串“ Data Source = x.x.x.x,1433”中是否具有端口 SQL Server Express默认为SQLEXPRESS实例名称 尝试使用。或
因此,请尝试使用。\ SQLEXPRESS或(本地)\ SQLEXPRESS作为服务器+实例名称。
如果这不起作用:您在Management Studio中使用什么服务器名称连接到SQL Server实例?用这个名字!
,尝试使用。 (点)作为实例名称