如何能使iis连接数据库

如何能使iis连接数据库,第1张

哥们,你要将你下载的斜面代码中连接数据库的代码改掉。

下面提供几种链接代码,你只要找到相似的代码,改掉就可以了,主要是看你的数据库是那种类型的。

1、MS Access数据库连接

用DSN连接并且没有用户名和密码:

<%

set conn = Server.CreateObject("ADODB.Connection")

conn.open "YourDSNName"

%>

用DSN连接并且有用户名和密码:

<%

set conn = Server.CreateObject("ADODB.Connection")

conn.open "YourDSNName","username","password"

%>

用实际的数据库绝对路径连接:

<%

Set conn = Server.CreateObject("ADODB.Connection")

Strconn="DRIVER={Microsoft Access Driver (*.mdb)}"

Strconn=Strconn &"DBQ=e:\yanhang\database.mdb"

conn.Open Strconn

%>

用实际的数据库相对路径连接:

<%

Set conn = Server.CreateObject("ADODB.Connection")

Strconn="DRIVER={Microsoft Access Driver (*.mdb)}"

Strconn=Strconn &"DBQ=" &Server.MapPath("/database/yanhang.mdb")

conn.Open Strconn

%>

2、MS SQL Server数据库连接

用DSN连接:

<%

set conn = Server.CreateObject("ADODB.Connection")

conn.open "DSN=MyDSNUID=userPWD=passwordDATABASE=databasename"

%>

不用DSN连接:

<%

Set conn = Server.CreateObject("ADODB.Connection")

DSNtemp="DRIVER={SQL Server}SERVER=ServerNameUID=USERPWD=passwordDATABASE=databasename"

conn.open DSNtemp

%>

3、FoxPro数据库连接

<%

Set Conn = Server.CreateObject("ADODB.connection")

ConnStr= "Driver=Microsoft Visual Foxpro DriverUID=userIDSourceType=DBCSourceDB=C:\yanhang\database.dbc"

Conn.Open ConnStr

%>

4、Oracle数据库连接:

<%

set conn=server.createobject("adodb.connection")

conn.cursorlocation=adUseClient

DSNTemp="Provider=MSDAORA.1Password=xxxxxUser ID=yanhangData Source=xxx.world"

conn.open DSNtemp

%>

public void CreateMDB()

{

//为了方便测试,数据库名字采用比较随机的名字,以防止添加不成功时还需要重新启动IIS来删除数据库。

string time = DateTime.Now.Year.ToString()

if (DateTime.Now.Month <10)//判断当前月是否小于10

{

time += "0" + DateTime.Now.Month

}

else

{

time += DateTime.Now.Month

}

time += DateTime.Now.Day

string apppath = System.Web.HttpContext.Current.Request.ApplicationPath

string dbName = System.Web.HttpContext.Current.Server.MapPath("\\" + apppath + "\\NewMDB\\CGMDB" + time + ".mdb")

ADOX.CatalogClass cat = new ADOX.CatalogClass()

cat.Create("Provider=Microsoft.Jet.OLEDB.4.0Data Source=" + dbName + "")

//ADODB.Connection cn = new ADODB.Connection()

//cn.Open("Provider=Microsoft.Jet.OLEDB.4.0Data Source=" + dbName + "", null, null, -1)

// cat.ActiveConnection = cn

ADOX.TableClass tbl = new ADOX.TableClass()

tbl.ParentCatalog = cat

tbl.Name = "WTMDBTable"

//增加一个文本字段

ADOX.ColumnClass col2 = new ADOX.ColumnClass()

col2.ParentCatalog = cat

col2.Name = "CheckSignNo"

col2.Properties["Jet OLEDB:Allow Zero Length"].Value = false

tbl.Columns.Append(col2, ADOX.DataTypeEnum.adVarChar, 50)

//增加一个文本字段

ADOX.ColumnClass col3 = new ADOX.ColumnClass()

col3.ParentCatalog = cat

col3.Name = "CarNo"

col3.Properties["Jet OLEDB:Allow Zero Length"].Value = false

tbl.Columns.Append(col3, ADOX.DataTypeEnum.adVarChar, 50)

//增加一个文本字段

ADOX.ColumnClass col4 = new ADOX.ColumnClass()

col4.ParentCatalog = cat

col4.Name = "CheckDate"

col4.Properties["Jet OLEDB:Allow Zero Length"].Value = false

tbl.Columns.Append(col4, ADOX.DataTypeEnum.adVarChar, 50)

//增加一个文本字段

ADOX.ColumnClass col5 = new ADOX.ColumnClass()

col5.ParentCatalog = cat

col5.Name = "TownClass"

col5.Properties["Jet OLEDB:Allow Zero Length"].Value = false

tbl.Columns.Append(col5, ADOX.DataTypeEnum.adVarChar, 50)

//增加一个文本字段

ADOX.ColumnClass col6 = new ADOX.ColumnClass()

col6.ParentCatalog = cat

col6.Name = "Address"

col6.Properties["Jet OLEDB:Allow Zero Length"].Value = false

tbl.Columns.Append(col6, ADOX.DataTypeEnum.adVarChar, 200)

//增加一个文本字段

ADOX.ColumnClass col7 = new ADOX.ColumnClass()

col7.ParentCatalog = cat

col7.Name = "Action"

col7.Properties["Jet OLEDB:Allow Zero Length"].Value = false

tbl.Columns.Append(col7, ADOX.DataTypeEnum.adVarChar, 50)

//增加一个文本字段

ADOX.ColumnClass col8 = new ADOX.ColumnClass()

col8.ParentCatalog = cat

col8.Name = "CarType"

col8.Properties["Jet OLEDB:Allow Zero Length"].Value = false

tbl.Columns.Append(col8, ADOX.DataTypeEnum.adVarChar, 50)

//增加一个文本字段

ADOX.ColumnClass col9 = new ADOX.ColumnClass()

col9.ParentCatalog = cat

col9.Name = "Mark"

col9.Properties["Jet OLEDB:Allow Zero Length"].Value = false

tbl.Columns.Append(col9, ADOX.DataTypeEnum.adVarChar, 50)

//设置主键

tbl.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "CheckSignNo", "", "")

cat.Tables.Append(tbl)

System.Runtime.InteropServices.Marshal.ReleaseComObject(tbl)

System.Runtime.InteropServices.Marshal.ReleaseComObject(cat)

tbl = null

cat = null

GC.WaitForPendingFinalizers()

GC.Collect()

}

以上代码可以完成创建一个MDB数据库了


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

原文地址: http://outofmemory.cn/bake/11849739.html

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

发表评论

登录后才能评论

评论列表(0条)

保存