刚刚入手VB.NET,对于SQL数据库操作遇到些小问题,请知情者帮忙.在线等~~~

sessisn 2008-10-03 12:16:51
Private Con As String = "<连接字符串>"'<连接字符串>可以自动生成
'ConnStr = "Provider=Sqloledb; User ID=sa; Password=123456; Initial Catalog=mytable; Data Source=127.0.0.1;"我把mssql的默认端口改为1434后,使用如下连接出错.ConnStr = "Provider=Sqloledb; User ID=sa; Password=123456; Initial Catalog=mytable; Data Source=127.0.0.1,1434;"

Private connection As SqlClient.SqlConnection
Private command As SqlClient.SqlCommand
Public Function DBopen() As Boolean '打开数据库
Try
connection = New SqlClient.SqlConnection
connection.ConnectionString = Con
connection.Open()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function
Public Function DBclose() As Boolean '关闭数据库
Try
connection = New SqlClient.SqlConnection
connection.ConnectionString = Con
connection.Close()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function
Private Con As String = "<连接字符串>"'<连接字符串>可以自动生成
'ConnStr = "Provider=Sqloledb; User ID=sa; Password=123456; Initial Catalog=mytable; Data Source=127.0.0.1;"我把mssql的默认端口改为1434后,使用如下连接出错.ConnStr = "Provider=Sqloledb; User ID=sa; Password=123456; Initial Catalog=mytable; Data Source=127.0.0.1,1434;"
Private connection As SqlClient.SqlConnection
Private command As SqlClient.SqlCommand
Public Function DBopen() As Boolean '打开数据库
Try
connection = New SqlClient.SqlConnection
connection.ConnectionString = Con
connection.Open()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function
Public Function DBclose() As Boolean '关闭数据库
Try
connection = New SqlClient.SqlConnection
connection.ConnectionString = Con
connection.Close()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function


上面程序是一个SQL数据库的连接,从网上找到的,现在不知道他具体怎么用?请高手们给简单明了易懂的利用上面代码写一个小例子,主要实现连接数据库以后可以使用查询语句并返回查询后的结果并且输出到一个文本框中.
...全文
91 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sessisn 2008-10-03
  • 打赏
  • 举报
回复
Private Con As String = " <连接字符串>"' <连接字符串>可以自动生成
'ConnStr = "Provider=Sqloledb; User ID=sa; Password=123456; Initial Catalog=mytable; Data Source=127.0.0.1;"我把mssql的默认端口改为1434后,使用如下连接出错.ConnStr = "Provider=Sqloledb; User ID=sa; Password=123456; Initial Catalog=mytable; Data Source=127.0.0.1,1434;"

Private connection As SqlClient.SqlConnection
Private command As SqlClient.SqlCommand
Public Function DBopen() As Boolean '打开数据库
Try
connection = New SqlClient.SqlConnection
connection.ConnectionString = Con
connection.Open()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function
Public Function DBclose() As Boolean '关闭数据库
Try
connection = New SqlClient.SqlConnection
connection.ConnectionString = Con
connection.Close()
Return True
Catch ex As Exception
MsgBox(ex.ToString)
Return False
End Try
End Function


现在我主要是想知道这段时间怎么用?如何结合这段打开数据库的代码进行查询半返回记录.谢谢.
panmeichen 2008-10-03
  • 打赏
  • 举报
回复
本答案没有检验大小写.
1,web.config:
<appSettings>
<add key="sqlServices" value="server=.;database=DBName;uid=sa;pwd=/>
</appSettings>
2,dal.cs:
//数据层
using System;
using System.Data;
using System.Data.SqlClient;
public class DB
{
//私有变量,数据库连接
protected SqlConnection Connection;
protected string Settings;

//构造函数
public DB()
{
//
// TODO: 在此处添加构造函数逻辑
//
Settings = Convert.ToString(ConfigurationManager.AppSettings["SqlServices"]);

}

//保护方法,打开数据库
private void Open()
{
//判断数据库连接是否存在
if (Connection == null)
{
//不存在,新建并打开
Connection = new SqlConnection(Settings);
Connection.Open();
}
else
{
//存在,判断是否处于关闭状态
if (Connection.State.Equals(ConnectionState.Closed))

Connection.Open(); //连接处于关闭状态,重新打开

}


}


//公有方法,关闭数据库
public void Close()
{
if (Connection.State.Equals(ConnectionState.Open))
{
Connection.Close();//连接处于打开状态,关闭连接
}
}

///<summary>
///析构函数,释放非托管资源
///</summary>
~DB()
{
try
{
if (Connection != null)
Connection.Close();
}
catch
{ }
try
{
Disposed();
}
catch { }
}


//公有方法,释放资源
public void Disposed()
{
if (Connection != null)//确保连接被关闭
{
Connection.Dispose();
Connection = null;
}

}
//公有方法,根据Sql语句,返回一个结果数据集
public DataSet GetDataSetSql(string XSqlString)
{
Open();
SqlDataAdapter adapter = new SqlDataAdapter(XSqlString, Connection);
DataSet ds = new DataSet();
adapter.Fill(ds);
Close();
return (ds);
}
}
3,UserDAL,cs(数据层,继承db.cs,bll类和数据库通信的类)
//用户登陆,判断密码是否正确
public bool IsPwdRight(string email, string pwd)
{
bool ispwdrightvalue = false;
string newSql = "select * from zpuser where email='" + email + "' and password='" + pwd + "'";
base.Open();
SqlCommand cmd = new SqlCommand(newSql, base.Connection);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
ispwdrightvalue = true;
}
else
{
ispwdrightvalue = false;
}
base.Close();//sdr使用完后立刻释放数据库连接
return ispwdrightvalue;
}
4,User.cs(业务逻辑层,和数据层通信)
public bool CheckPwd(string email, string pwd)
{
UserDAL userDAL = new UserDAL();

if (userDAL.IsPwdRight(email,pwd))
return true;
else
return false;

}
5,youweb.aspx.cs (表示层,和业务逻辑层通信)
//btnlogin_Click登陆提交按钮
protected void btnlogin_Click(object sender, EventArgs e)
{
if (IsThroughCheckStep1ByServer())//如果通过了服务器端验证,那么保存数据,此方法略
{

//检查用户名密码是否正确
if (IfRightUser(txt_email.Text, txt_password.Text))
{
//略
}
else
{
//略
}


}
}

public static bool IfRightUser(string email, string password)
{
User user=new User();
if (user.CheckPwd(email, password))
return true;
else
return false;
}
davidblus 2008-10-03
  • 打赏
  • 举报
回复
我也想要

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧