程序源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;namespace Login
{
class LinkDataBase
{
private string strSQL;
//与SQL Server的连接字符串设置
private string connectionString = @"Data Source=LocalHost;Integrated Security=true;Database=Senic";
//与数据库的连接
private SqlConnection myConnection; private SqlCommandBuilder sqlCmdBld;
private DataSet ds = new DataSet();
private SqlDataAdapter da; public LinkDataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
} ///////////////////////////////// 操作脱机数据库(创建了该类的实例时直接用) ///////////////////////////////////////////////////// //根据输入的SQL语句检索数据库数据
public DataSet SelectDataBase(string tempStrSQL, string tempTableName)
{
this.strSQL = tempStrSQL;
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.ds.Clear();
// this.ds.Reset();
this.da.Fill(ds,tempTableName);
this.myConnection.Close();
da.Dispose();
return ds;//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
} //数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDataBase(DataSet changedDataSet, string tableName)
{
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.sqlCmdBld = new SqlCommandBuilder(da);
this.da.Update(changedDataSet, tableName);
return changedDataSet;//返回更新了的数据库表
} ///////////////////////////////// 直接操作数据库(未创建该类的实例时直接用) ///////////////////////////////////////////////////// //检索数据库数据(传字符串,直接操作数据库)
public DataTable SelectDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
DataSet tempDataSet = new DataSet();
this.da = new SqlDataAdapter(tempStrSQL, this.myConnection);
this.da.Fill(tempDataSet); return tempDataSet.Tables[0];
} //数据库数据更新(传字符串,直接操作数据库)
public int UpdateDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会自动打开关闭连接
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL, this.myConnection);
int intNumber = tempSqlCommand.ExecuteNonQuery();//返回数据库中影响的行数
myConnection.Close();
return intNumber;
}
public void InsertDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL, this.myConnection);
int number = tempSqlCommand.ExecuteNonQuery();
myConnection.Close();
}
}
}
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace Login
{
public partial class Form1 : Form
{
private LinkDataBase link = new LinkDataBase();
private DataSet ds = new DataSet();
private string tableName = "login"; private string name = "";
private string password = ""; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
name = textBox1.Text;
password = textBox2.Text;
} private void button1_Click(object sender, EventArgs e)
{
name = textBox1.Text;
password = textBox2.Text;
string strSQL = "select * from login where name ='" +name+"'";
this.ds = link.SelectDataBase(strSQL,tableName);
if (ds.Tables[tableName].Rows.Count > 0)
{
MessageBox.Show("该用户已存在!");
}
else
{
string strSQL1 = "insert into login values('"+name+"','"+password+"')";
link.InsertDataBase(strSQL1);
MessageBox.Show("添加成功!");
}
}
}
}程序执行过程中抛出异常,指在这句上:this.da.Fill(tempDataSet);
错误提示: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)注:c#程序连sql时,sql是以windows身份验证方式登录的
连接语句为:private string connectionString = @"Data Source=LocalHost;Integrated Security=true;Database=Senic";
这样写对吗
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;namespace Login
{
class LinkDataBase
{
private string strSQL;
//与SQL Server的连接字符串设置
private string connectionString = @"Data Source=LocalHost;Integrated Security=true;Database=Senic";
//与数据库的连接
private SqlConnection myConnection; private SqlCommandBuilder sqlCmdBld;
private DataSet ds = new DataSet();
private SqlDataAdapter da; public LinkDataBase()
{
//
// TODO: 在此处添加构造函数逻辑
//
} ///////////////////////////////// 操作脱机数据库(创建了该类的实例时直接用) ///////////////////////////////////////////////////// //根据输入的SQL语句检索数据库数据
public DataSet SelectDataBase(string tempStrSQL, string tempTableName)
{
this.strSQL = tempStrSQL;
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.ds.Clear();
// this.ds.Reset();
this.da.Fill(ds,tempTableName);
this.myConnection.Close();
da.Dispose();
return ds;//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
} //数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDataBase(DataSet changedDataSet, string tableName)
{
this.myConnection = new SqlConnection(connectionString);
this.da = new SqlDataAdapter(this.strSQL, this.myConnection);
this.sqlCmdBld = new SqlCommandBuilder(da);
this.da.Update(changedDataSet, tableName);
return changedDataSet;//返回更新了的数据库表
} ///////////////////////////////// 直接操作数据库(未创建该类的实例时直接用) ///////////////////////////////////////////////////// //检索数据库数据(传字符串,直接操作数据库)
public DataTable SelectDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
DataSet tempDataSet = new DataSet();
this.da = new SqlDataAdapter(tempStrSQL, this.myConnection);
this.da.Fill(tempDataSet); return tempDataSet.Tables[0];
} //数据库数据更新(传字符串,直接操作数据库)
public int UpdateDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
//使用Command之前一定要先打开连接,后关闭连接,而DataAdapter则会自动打开关闭连接
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL, this.myConnection);
int intNumber = tempSqlCommand.ExecuteNonQuery();//返回数据库中影响的行数
myConnection.Close();
return intNumber;
}
public void InsertDataBase(string tempStrSQL)
{
this.myConnection = new SqlConnection(connectionString);
myConnection.Open();
SqlCommand tempSqlCommand = new SqlCommand(tempStrSQL, this.myConnection);
int number = tempSqlCommand.ExecuteNonQuery();
myConnection.Close();
}
}
}
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace Login
{
public partial class Form1 : Form
{
private LinkDataBase link = new LinkDataBase();
private DataSet ds = new DataSet();
private string tableName = "login"; private string name = "";
private string password = ""; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
name = textBox1.Text;
password = textBox2.Text;
} private void button1_Click(object sender, EventArgs e)
{
name = textBox1.Text;
password = textBox2.Text;
string strSQL = "select * from login where name ='" +name+"'";
this.ds = link.SelectDataBase(strSQL,tableName);
if (ds.Tables[tableName].Rows.Count > 0)
{
MessageBox.Show("该用户已存在!");
}
else
{
string strSQL1 = "insert into login values('"+name+"','"+password+"')";
link.InsertDataBase(strSQL1);
MessageBox.Show("添加成功!");
}
}
}
}程序执行过程中抛出异常,指在这句上:this.da.Fill(tempDataSet);
错误提示: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误。未找到或无法访问服务器。请验证实例名称是否正确并且 SQL Server 已配置为允许远程连接。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)注:c#程序连sql时,sql是以windows身份验证方式登录的
连接语句为:private string connectionString = @"Data Source=LocalHost;Integrated Security=true;Database=Senic";
这样写对吗
解决方案 »
- #中一般做个会员审核是干什么用的,急、急、急、、、、
- 毕设的代码问题 谁帮我解答一下啊?每句话都什么意思啊
- Access操作问题-新人求助
- 这段代码真的看不明白,请朋友们指点!
- [求助]如何在C#中实现从文件直接读取数据结构
- C#创建数据库的时候,怎么判断数据库名是否重复
- 帮忙看看这个代码怎么写?
- 各位大侠,从XML文档解析后,几万条数据插入SQL Server,除了循环逐条插入,谁有其他好的方案呢?请大侠支招。
- 用C#语言进行图像处理的学习
- .net里面怎么访问客户端的打印机
- 请教大虾们,这段代码有什么问题么?为什么没效果啊?代码都执行了,传的参数也对!
- winform如何做出QQ主菜单的效果
建议你做如下步骤仔细检查一下:
1. 确保SQL Server 已配置为允许远程连接。
2. 确保连接字符串正确。
this.myConnection = new SqlConnection(connectionString);
DataSet tempDataSet = new DataSet();
myConnection.connect();
this.da = new SqlDataAdapter(tempStrSQL, this.myConnection);
this.da.Fill(tempDataSet); return tempDataSet.Tables[0];