求一段最简单的aspx *** 作数据库代码

求一段最简单的aspx *** 作数据库代码,第1张

/// <summary>

///

/// </summary>

/// <param name="sql">需要执行的sql语句</param>

/// <returns>返回dataset</returns>

public DataSet execSql(string sql)

{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString)

con.Open()

SqlDataAdapter da = new SqlDataAdapter(sql, con)

DataSet ds = new DataSet()

da.Fill(ds)

return ds

}

myconnectionstring 是你在web.config文件里配置的连接名称

1、上一个网站不能看出它采用的后台服务器是什么.

2、如果给一个学校做二级页面是不是要求采用的数据库和原来一致的问题还是看具体情况。但不管怎么说用一样的数据库总比不同的数据库要好,至少两个数据库交互容易点儿,为他们做交互接口也更容易

3、如果访问量小,用access就可以,如果比较打则用sqlserver。用微软的数据库更容易。如果从想省钱用mysql也是不错的选择。企业级应用:oracle,db2

基础类库:

Imports Microsoft.VisualBasic

'执行Access语句的封装类。

Public Class AccessExecClass

Private conn As Data.OleDb.OleDbConnection

Private cmd As Data.OleDb.OleDbCommand

'创建类对象,并连接到数据库。

Sub New()

If openAccess Then

Else

msgbox("open Access Fail.")

End If

End Sub

'创建类对象,并连接到数据库。'输入带目录名称的数据库文件名称:dbFileName,例如:“\DB数据库\企业数据库.aspx”

Sub New(ByVal dbFileName As String)

If OpenAccess(dbFileName) Then

Else

MsgBox("open Access Fail.")

End If

End Sub

Function OpenAccess() As Boolean

OpenAccess = False

'检验用户的帐号密码是否正确

Dim provider As String '= ConfigurationManager.ConnectionStrings("ConnectionString").ProviderName

provider = "Provider=Microsoft.Jet.OLEDB.4.0"

Dim database As String

database = "Data Source=" &System.Web.HttpContext.Current.Server.MapPath("\数据库.aspx") &"Persist Security Info=true"

conn = New Data.OleDb.OleDbConnection(provider &"" &database)

conn.Open()

OpenAccess = True

End Function

Function OpenAccess(ByVal dbFileName As String) As Boolean

OpenAccess = False

'检验用户的帐号密码是否正确

Dim provider As String '= ConfigurationManager.ConnectionStrings("ConnectionString").ProviderName

provider = "Provider=Microsoft.Jet.OLEDB.4.0"

Dim database As String

database = "Data Source=" &System.Web.HttpContext.Current.Server.MapPath(dbFileName) &"Persist Security Info=true"

conn = New Data.OleDb.OleDbConnection(provider &"" &database)

conn.Open()

OpenAccess = True

End Function

'关闭数据库。

Sub CloseAccess()

cmd = Nothing

conn.Close()

End Sub

'执行一条Access语句,执行成功就返回True。

Function ExecAccessString(ByVal AccessStr As String) As Boolean

cmd = New Data.OleDb.OleDbCommand(AccessStr, conn)

cmd.ExecuteNonQuery()

'Dim aa As Data.OleDb.OleDbDataReader = cmd.ExecuteReader

ExecAccessString = True

End Function

'返回读取到的数据,注意是用地址传递方式返回

Sub ExecAccessString(ByVal AccessStr As String, ByRef AccessReader As Data.OleDb.OleDbDataReader)

cmd = New Data.OleDb.OleDbCommand(AccessStr, conn)

cmd.ExecuteNonQuery()

AccessReader = cmd.ExecuteReader

End Sub

End Class

登陆代码:

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton1.Click

Dim wl As New WebLog'用户登录记录开始,仅对购买了Log 功能的用户,才开放此功能,否则不会实际记录Log

Dim sql As String

Dim aDB As New AccessExecClass

Dim mypw As String

Label1.Visible = False

Label2.Visible = False

Label3.Visible = False

Label4.Visible = False

'没有输入时,不能进行登陆

If TextBox1.Text = "" Then

Label1.Visible = True

Return

End If

If TextBox2.Text = "" Then

Label2.Visible = True

Return

End If

If TextBox3.Text = "" Then

Label3.Visible = True

Return

End If

If TextBox3.Text = TextBox4.Text Then

Else

Label3.Visible = True

Return

End If

Dim iRnd As Integer = Rnd(10) * 100

TextBox3.Text = ""

TextBox4.Text = iRnd.ToString

'Dim iRnd As Integer = Rnd(10) * 100

'注意:此处需要加入防止注入分析,包含'的肯定是特别语句,讲拒绝.

If TextBox1.Text.IndexOf("'") >0 Or TextBox2.Text.IndexOf("'") >0 Then

TextBox1.Text = "不能输入非法字符,将强制清空"

TextBox2.Text = ""

Else

Dim pw As New MD5EnCode(13, 19) ' '2次加密

sql = "select 权限组号,ID , 密码 from 编辑人数据 where 编辑姓名='" + TextBox1.Text + "'"

Dim aa As Data.OleDb.OleDbDataReader '= cmd.ExecuteReader

aDB.ExecAccessString(sql, aa)

If aa.Read() Then

'密码正确()

'sql = aa(2)'需要特别注意:只能单向解密,不能直接转换

mypw = pw.simDeCode(pw.simDeCode(aa(2)))

If TextBox2.Text = mypw Then

Session("LOGIN_Name") = TextBox1.Text '设置登录后的用户名称

Session("Login_Group") = aa(0) 'cmd.ExecuteScalar '设置登录后的权限级别

Session("Login_ID") = aa(1) 'cmd.ToString

'cmd = Nothing

'conn.Close()

Call wl.NewLog("登录", "管理员成功登录")

Response.Redirect("../WebManages/Default.ASPX")

Else

'密码错误()

sql = sql.Replace("'", "【单引号】")

sql = sql.Replace(",", "【逗号】")

Call wl.NewLog("登录", "管理员失败的登录", TextBox1.Text, sql.Replace("""", "【双引号】"), "")

Label4.Visible = True

End If

Else

'密码错误()

sql = sql.Replace("'", "【单引号】")

sql = sql.Replace(",", "【逗号】")

Call wl.NewLog("登录", "管理员失败的登录", TextBox1.Text, sql.Replace("""", "【双引号】"), "")

Label4.Visible = True

End If

End If

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9986433.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存