asp 创建mssql数据库

asp 创建mssql数据库,第1张

方法一:

<%

' 使用ADODBConnect对象创建

Dim oConn

Dim sDatabaseName

sDatabaseName = "CodeCreateDB"

Set oConn = ServerCreateObject("ADODBConnection")

oConnOpen "Provider=SQLOLEDB;Data Source=(local);User ID=sa;Password=;"

oConnExecute "CREATE DATABASE " & sDatabaseName

%>

方法二:

<%@ Language=VBScript %>

<%

' 使用 SQLDMO Object 对象库

Dim oSQLServer

Dim oDatabase

Dim oDBFileData

Dim oLogFile

Dim sDatabaseName

Dim sDatapath

'数据库名字

sDatabaseName = "CodeCreateDBTest"

'数据文件保存路径

sDatapath = "D:"

'创建Sql Server对象并进行链接,(local)处为Server名字

Set oSQLServer = ServerCreateObject("SQLDMOSQLServer")

' oSQLServerLoginSecure = True ' 使用集成验证integrated security

' oSQLServerConnect "(local)"

oSQLServerConnect "(local)", "sa", "" ' 使用标准验证 standard security

' 创建Database对象

Set oDatabase = ServerCreateObject("SQLDMODatabase")

oDatabaseName = sDatabaseName

' 创建db文件对象

Set oDBFileData = ServerCreateObject("SQLDMODBFile")

With oDBFileData

Name = sDatabaseName & "_data"

PhysicalName = sDatapath & "" & sDatabaseName & "_datamdf"

PrimaryFile = True

FileGrowthType = SQLDMOGrowth_MB

FileGrowth = 1

End With

' 创建日志文件对象

Set oLogFile = ServerCreateObject("SQLDMOLogFile")

With oLogFile

Name = sDatabaseName & "_log"

PhysicalName = sDatapath & "" & sDatabaseName & "_logldf"

End With

'把DB文件对象和日志文件对象添加到DataBase数据库对象

oDatabaseFileGroups("PRIMARY")DBFilesAdd oDBFileData

oDatabaseTransactionLogLogFilesAdd oLogFile

' 把数据库添加到Sql server(create the database)

oSQLServerDatabasesAdd oDatabase

' 关闭连接

oSQLServerClose

' 释放对象

Set oLogFile = Nothing

Set oDBFileData = Nothing

Set oDatabase = Nothing

Set oSQLServer = Nothing

%>

创建表就执行connexecute("create table [table](id int identity primary key)")

1、连入SQLPlus

以system/manager用户登录,

SQL> conn system/manager

创建新的用户:如user1/pass1,赋予connect,resource权限。

SQL> grant connect,resource to user1 identified by pass1;

SQL> conn user1/pass1

SQL> create table test(a date);

Oracle数据库的实例名,本例:oradb

Oracle用户名,本例:user1

Oracle主机字符串:oradb

tnsnamesora文件中

ORADB =

(DESCRIPTION =

(ADDRESS_LIST =

(ADDRESS = (PROTOCOL = TCP)(HOST = 192111)(PORT = 1521))

)

(CONNECT_DATA =

(SERVICE_NAME = oradb)

)

)

2、在IIS默认>

不知道你为什么要存数组?

你可以在数据表中 建个字符串字段 strArray

添加的时候如: int [] intArr=new int[][1,2,3,4 };

string result=StringEmpty;

for(int i=0;i<intArrlength;i++)

{

if(i==(intArrlength-1)) result+=intArr[i];

else result+=intArr[i]+"_" ; //可以自己定义一个分隔符

}

添加数据时,把result 的值 添到数据库中

获取时 直接 用 split('_')获取就行了。

要显示的页面设置

<asp:datalist id="MyList" repeatcolumns="2" borderwidth="0" runat="server">

<ItemTemplate>

<table>

<tr>

<td>

<img width=40 height=40 src='<%# DataBinderEval(ContainerDataItem, "ImgId", "Imgaspxid=")%>'>

</td>

</tr>

</table>

</ItemTemplate>

</asp:datalist>

后台代码

OleDbConnection objConn = new OleDbConnection();

objConnConnectionString = @"Provider=MicrosoftJetOLEDB40;Data Source=" + ServerMapPath("Datamdb");

objConnOpen();

OleDbCommand objCmd = new OleDbCommand("select from tbl_Image", objConn);

OleDbDataAdapter da = new OleDbDataAdapter(objCmd);

DataSet ds = new DataSet();

daFill(ds, "aa");

MyListDataSource = dsTables["aa"];

MyListDataBind();

objConnClose();

imgaspx页面代码

int id = intParse(Request["id"]ToString());

OleDbConnection objConn = new OleDbConnection();

objConnConnectionString = @"Provider=MicrosoftJetOLEDB40;Data Source=" + ServerMapPath("Datamdb");

objConnOpen();

OleDbCommand objCmd = new OleDbCommand("select top 1 Image from tbl_Image where ImgId=@id order by ImgId desc",objConn);

objCmdParametersAdd("@id", OleDbTypeInteger);

objCmdParameters["@id"]Value = id;

OleDbDataReader objDr = objCmdExecuteReader();

while(objDrRead())

{

ResponseBinaryWrite((byte[])objDr["Image"]);

}

objDrClose();

objConnClose();

(第一个小时)

1:首先建立一个access数据库保存为 xhnewmdb 表名为aa

字段如下:

id 自动排序

name 文本

content 文本

xhtime 日期 默认值 now()

title 文本

2:打开DW 建立文件connasp文件

代码如下:

CODE:

<%

set conn=servercreateobject("adodbconnection")

connopen "driver={microsoft access driver (mdb)};dbq="&servermappath("xhnewmdb")

%>

这段语句是连接数据库的语句 set conn=servercreateobject("adodbconnection")创建一个connection对象 用该对象的open方法打开数据库

driver={microsoft access driver (mdb)};这句语句是access的驱动

dbq="&servermappath("xhnewmdb")这句是数据库的路径

建立connasp是为了方便接下来的教学。。。

我们可以任何要用到打开数据库连接的文件里加

<!--#include file="connasp"-->就可以调用

是不是很方便。第一个小时就学这么多。。。当你弄明白上面的数据库还有代码后。。

就继续跟着我学吧!

(第2个小时)

现在来学把数据库的信息输出来

下面先建立一个主文件indexasp

代码如下

CODE:

<!--#include file="connasp"-->

<%

exec="select from aa order by id desc "

set rs=servercreateobject("adodbrecordset")

rsopen exec,conn,1,1

%>

<table width="628" height="24" border="1" align="center" cellpadding="1" cellspacing="0">

<%

if rseof and rsbof then

responsewrite("暂时没有文章")

else

do while not rseof

%>

<tr>

<td width="66" height="22" ><%=rs("id")%></td>

<td width="66" ><%=rs("name")%></td>

<td width="66" ><%=rs("content")%></td>

<td width="273" ><%=rs("xhtime")%></td>

<td width="53" ><%=rs("title")%></td>

<td><a href="modifyaspid=<%=rs("id")%>" target="_self">编辑</a></td>

<td width="32" ><a href="delaspid=<%=rs("id")%>">删除</a></td>

</tr>

<%

rsmovenext

loop

end if

%>

</table>

<%

rsclose

set rs=nothing

connclose

set conn=nothing

%>

<td><a href="addasp">添加</a></td>

[Copy to clipboard]

代码解释:

(1)<%

exec="select from aa order by id desc "

set rs=servercreateobject("adodbrecordset")

rsopen exec,conn,1,1

%>

上面的代码是创建一个recordset对象。。该对象是用来打开数据库中的表的。。也就是打开aa

exec="select from aa order by id desc " 这句的意思是查找表aa中所有信息并倒序排列

信息 号表示aa中所有的信息 order by id 就是aa中id字段 desc 是倒序的意思

rsopen exec,conn,1,1 这句话的意思是用recordset对象来打开conn中的表

(2)

<%

if rseof and rsbof then

responsewrite("暂时没有文章")

else

do while not rseof

%>

上面是if语句。。if else语句是相当重要的。。

希望一些初学者要认真的研究一下if else语句。。不可心急。基础才是硬道理

rseof 的意思是aa表中的最后一条记录 那么rsbof就是第一条记录

整个语句用中文翻译就是 当aa表中的最后和最前一条信息没有的时候。。就输出暂时没有文章

或则就循环到eof最后一条信息。。

(3)<tr>

<td width="66" height="22" ><%=rs("id")%></td>

<td width="66" ><%=rs("name")%></td>

<td width="66" ><%=rs("content")%></td>

<td width="273" ><%=rs("xhtime")%></td>

<td width="53" ><%=rs("title")%></td>

<td><a href="modifyaspid=<%=rs("id")%>" target="_self">编辑</a></td>

<td width="32" ><a href="delaspid=<%=rs("id")%>">删除</a></td>

</tr>

上面的代码比较容易理解。。<%=rs("id")%>这个用法是输出数据库字段id

<a href="modifyaspid=<%=rs("id")%>" target="_self">编辑</a>这段很重要。。

我来解释一下。。在ie窗口点 编辑 的时候。。页面会跳转到modifyasp的文件里的id

并进行相应的处理。。这里很难解释清楚。。在接下来的例子中再慢慢解说

(4)

<%

rsmovenext

loop

end if

%>

这句是循环结束的语句

rsmovenext 这句是游标向下指的意思

(5)

<%

rsclose

set rs=nothing

connclose

set conn=nothing

%>

这句是关闭数据库的语句

用完数据库后要记得关闭数据库。。以免占用资源

养成随手关门的好习惯

就学到这里。。好好消化上面的代码。。心急吃不了热豆腐。。

一步一个脚印最塌实

(第3个小时)

现在来学添加数据到数据库 需要两个文件来实现

(1)先建立一个提交表单addasp

代码如下:

CODE:

<html>

<head>

<meta >

以上就是关于asp 创建mssql数据库全部的内容,包括:asp 创建mssql数据库、求一个ASP连接到ORACLE数据库的样本程序(200分)、asp.net如何把一个数组添加到数据库中.举个例子谢谢等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

原文地址:https://54852.com/sjk/9792225.html

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

发表评论

登录后才能评论

评论列表(0条)

    保存