问题出错```不支持关键字: “simon-pc\sqlexpress\sqlexpress;initial catalog”
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 测试2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Data Source=L_RYAN-PC\SQLEXPRESS;Initial Catalog=test;Integrated Security=True
string ConnectionString = "SIMON-PC\\SQLEXPRESS\\SQLEXPRESS;Initial Catalog=Hospetal;Integrated Security=True";
using(SqlConnection conn = new SqlConnection())//创建连接对象
{
conn.ConnectionString = ConnectionString;//给连接对象的连接路径赋值
conn.Open();//运行连接对象,即是连接数据库
SqlCommand cmd = new SqlCommand();//创建命令对象
cmd.Connection = conn;//命令对象通过连接对象连接数据库发送命令
cmd.CommandText = "insert into 病人 values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";//命令对象发送命令的原文本
int i = cmd.ExecuteNonQuery();//运行命令对象
MessageBox.Show("插入成功!");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
} 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 测试2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Data Source=L_RYAN-PC\SQLEXPRESS;Initial Catalog=test;Integrated Security=True
string ConnectionString = "SIMON-PC\\SQLEXPRESS\\SQLEXPRESS;Initial Catalog=Hospetal;Integrated Security=True";
using(SqlConnection conn = new SqlConnection())//创建连接对象
{
conn.ConnectionString = ConnectionString;//给连接对象的连接路径赋值
conn.Open();//运行连接对象,即是连接数据库
SqlCommand cmd = new SqlCommand();//创建命令对象
cmd.Connection = conn;//命令对象通过连接对象连接数据库发送命令
cmd.CommandText = "insert into 病人 values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";//命令对象发送命令的原文本
int i = cmd.ExecuteNonQuery();//运行命令对象
MessageBox.Show("插入成功!");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
} 展开
1个回答
展开全部
string ConnectionString = "server=SIMON-PC\\SQLEXPRESS\\SQLEXPRESS;Initial Catalog=Hospetal;Integrated Security=True";
追问
那个错误好了,但是conn.Open();报错了``麻烦帮忙解决下``
在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: SQL 网络接口, error: 26 - 定位指定的服务器/实例时出错)
追答
连接数据库出错:你电脑上安装了SQL server没有?有安装的话,用SQL管理器连接进去,然后复制一下服务器的名称。
比如 SIMON-PC\\SQLEXPRESS\\SQLEXPRESS ,这个是你的SQL实例名称吗?
或者试试发在下面这个语句:
string ConnectionString = "server=.;Initial Catalog=Hospetal;Integrated Security=True";
或者
string ConnectionString = "server=.\sqlexpress;Initial Catalog=Hospetal;Integrated Security=True";
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询