using System;
using System.Data;
using System.Data.SqlClient;namespace OASystem.include
{
/// <summary>
/// DBLink 的摘要说明。
/// </summary>
public class DBLink
{
private SqlConnection myConnection;
private SqlCommand MyCommand = new SqlCommand();
private SqlDataAdapter myAdapter;
/// <summary>
/// <retvalue>错误信息</retvalue>
/// </summary>
public string errorMsg;
/// <summary>
/// <retvalue>连接数据库字符串,用SystemParameter.GetConnectionString方法取得</retvalue>
/// </summary>
private string connectionString = SystemParameter.GetConnectionString;
/// <summary>
/// <retvalue>构造函数</retvalue>
/// </summary>
public DBLink()
{
//
// TODO: 在此处添加构造函数逻辑
//
} /// <summary>
/// 打开连接数据库对象
/// </summary>
public void open()
{
try
{
myConnection = new SqlConnection(connectionString);
myConnection.Open();
}
catch (SqlException e)
{
errorMsg = e.Message.ToString();
}
} /// <summary>
/// 发送一个sql,返回一个查询结果集的DataSet
/// <param name="Sql">需要查询的string类型的SQL语句</param>
/// <param name="ds">DataSet类型对象</param>
/// <param name="tableName">填充在ds中的string类型的Table名称</param>
/// <retvalue>发送一个sql,返回一个查询结果集的DataSet</retvalue>
/// </summary>
/// <example> This sample shows how to call the executeQueryReturnDataSet method.
/// <code>
/// public class test : System.Web.UI.Page
/// {
/// private DataSet ds;
/// private void Page_Load(object sender, System.EventArgs e)
/// {
/// ds = new DataSet();
/// ds = dblink.executeQueryReturnDataSet("select * from Topic_Code_T",ds,"topiccode");
/// this.DataGrid1.DataSource = ds.Tables["topiccode"].DefaultView;
/// this.DataBind();
/// }
/// }
/// </code>
/// </example>
public DataSet executeQueryReturnDataSet(string Sql,DataSet ds,string tableName)
{
try
{
myConnection = new SqlConnection(connectionString);
myConnection.Open();
myAdapter = new SqlDataAdapter(Sql,myConnection);
myAdapter.Fill(ds,tableName);
myConnection.Close();
}
catch (SqlException e)
{
errorMsg = e.Message.ToString();
}
return ds;
}
可能太多了,你慢慢看看
using System.Data;
using System.Data.SqlClient;namespace OASystem.include
{
/// <summary>
/// DBLink 的摘要说明。
/// </summary>
public class DBLink
{
private SqlConnection myConnection;
private SqlCommand MyCommand = new SqlCommand();
private SqlDataAdapter myAdapter;
/// <summary>
/// <retvalue>错误信息</retvalue>
/// </summary>
public string errorMsg;
/// <summary>
/// <retvalue>连接数据库字符串,用SystemParameter.GetConnectionString方法取得</retvalue>
/// </summary>
private string connectionString = SystemParameter.GetConnectionString;
/// <summary>
/// <retvalue>构造函数</retvalue>
/// </summary>
public DBLink()
{
//
// TODO: 在此处添加构造函数逻辑
//
} /// <summary>
/// 打开连接数据库对象
/// </summary>
public void open()
{
try
{
myConnection = new SqlConnection(connectionString);
myConnection.Open();
}
catch (SqlException e)
{
errorMsg = e.Message.ToString();
}
} /// <summary>
/// 发送一个sql,返回一个查询结果集的DataSet
/// <param name="Sql">需要查询的string类型的SQL语句</param>
/// <param name="ds">DataSet类型对象</param>
/// <param name="tableName">填充在ds中的string类型的Table名称</param>
/// <retvalue>发送一个sql,返回一个查询结果集的DataSet</retvalue>
/// </summary>
/// <example> This sample shows how to call the executeQueryReturnDataSet method.
/// <code>
/// public class test : System.Web.UI.Page
/// {
/// private DataSet ds;
/// private void Page_Load(object sender, System.EventArgs e)
/// {
/// ds = new DataSet();
/// ds = dblink.executeQueryReturnDataSet("select * from Topic_Code_T",ds,"topiccode");
/// this.DataGrid1.DataSource = ds.Tables["topiccode"].DefaultView;
/// this.DataBind();
/// }
/// }
/// </code>
/// </example>
public DataSet executeQueryReturnDataSet(string Sql,DataSet ds,string tableName)
{
try
{
myConnection = new SqlConnection(connectionString);
myConnection.Open();
myAdapter = new SqlDataAdapter(Sql,myConnection);
myAdapter.Fill(ds,tableName);
myConnection.Close();
}
catch (SqlException e)
{
errorMsg = e.Message.ToString();
}
return ds;
}
可能太多了,你慢慢看看
解决方案 »
- .net发布网站的时候 出现的问题 求解决办法
- TreeNode控件有获取节点级别的属性吗,没有的话,怎么实现呢
- 关于get和set在webservice里怎么用?
- 还有个问题,我在网页里放的activex控件,,要怎样才能自动(提示)在客户端安装?
- C# 跪求 SharpPcap 3.6 发送UDP封包代码
- 我想在客户端页面上即时显示服务器时间。
- 布尔型和字符串型数据能否相互转化
- 继续讨论: DATAGRID一个带有 分页功能并能多行编辑,多行删除的(编辑与删除可跨页进行)
- 为什么我无法出发imagebutton的事件呢?
- Centos的docker容器应用中无法访问宿主机mysql数据库
- 问个简单的问题!关于修改的
- 急问在服务器端怎样才能得到当前页面的所有HTML代码?(在线等)
<configuration>
<!-- 定义数据库数据库连接串参数 -->
<appSettings>
<add key="pubs" value=
"server=(local) \NetSDK;database=pubs;Trusted_Connection=yes"/>
</appSettings>
<system.web>
</system.web>
</configuration>获得数据库连接串:
string connStr=System.Configuration.ConfigurationSettings.AppSettings[pubs];
SqlConnection MyConnection = New SqlConnection(connStr);
数据库连接字符串在Web.Config中定义:
<configuration>
<!-- 定义数据库数据库连接串参数 -->
<appSettings>
<add key="pubs" value=
"server=(local) \NetSDK;database=pubs;Trusted_Connection=yes"/>
</appSettings>
<system.web>
</system.web>
</configuration>获得数据库连接串:
string connStr=System.Configuration.ConfigurationSettings.AppSettings["pubs"];
SqlConnection MyConnection = New SqlConnection(connStr);
补充
MyConnection.Open();
“/”应用程序中的服务器错误。
--------------------------------------------------------------------------------SQL Server 不存在或访问被拒绝。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: SQL Server 不存在或访问被拒绝。源错误:
行 29: newsid = Request.Params("id")
行 30: MyConnection =New SqlConnection("Initial Catalog=mydb;Data Source=(local)\NetSDK;Integrated Security=SSPI;")
行 31: MyConnection.Open()
行 32: Dim strCom As String= "SELECT biaoti ,zhaizi ,neirong,img,counter ,shijian FROM news where id="+newsid
行 33: ds= new DataSet()
源文件: c:\inetpub\wwwroot\aspx\ch10\show1.aspx 行: 31
不知你有没有安装Microsoft .NET 框架 SDK 快速入门中的数据库服务器?下面这个地址或许对你有帮助:
http://expert.csdn.net/Expert/topic/1519/1519483.xml?temp=.5137293
行 30: MyConnection =New SqlConnection("Initial Catalog=mydb;Data Source=localhost;Integrated Security=SSPI;")出现错误是;用户 'ZHANGLIN\ASPNET' 登录失败。这个怎么解决?
一,应用Sqlserver验证;
二,添加'ZHANGLIN\ASPNET' 这个用户。