B brw Medlem sedan juli 2000 619 inlägg Frågan 17 okt. 2002 06:24 #1 Jag har följande kod för att ta fram fältnamn och datatyp från databasen... men när det är VarChar (SQL-server) eller Text (Access) så skulle jag även vilja få fram teckenlängd på datafältet... Går detta på något vis?
Set RS = Conn.OpenSchema(22)
Dim arrData(14, 1)
cLoop = 0
Do Until RS.EOF
If Not IsEmpty(RS("Type_Name")) Then
arrData(cLoop, 0) = RS("Type_Name")
arrData(cLoop, 1) = RS("Data_Type")
cLoop = cLoop + 1
End If
RS.MoveNext
Loop
L LarsG Medlem sedan dec. 2000 12 464 inlägg maxlen = rs("COLUMN_SIZE")
B brw Medlem sedan juli 2000 619 inlägg Hmm jag får det inte att fungera... här är hela koden... men det verkar inte som om den använder openschema
Det verkar som om den bara plockar ut från denna: For Each Item In RS.Fields
Hur ska jag få med storleken på fältet på varje kolumn som är text?
<% Response.Buffer = True %>
<!--#include file="dbpath/db_path.asp"-->
<%
Tabell = Request.QueryString("Tabell")
Set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBPath
If Request.Form("Func") = "Update" Then
Tabell = Request.Form("Tabell")
SQLStmt = "Alter Table "& Tabell &" "
'Falt0 Falt1 VARCHAR (50) DEFAULT '0' not null "
cLoop = 0
For Each Item In Request.Form
If Item <> "Tabell" And Item <> "Func" And Left(Item, 3) = "Old" Then
cLoop = cLoop + 1
NewShit = "New_" & Request.Form(Item)
DataTyp = "DataTyp_" & Request.Form(Item)
SQLStmt = SQLStmt & "Modify (" & Request.Form(Item) & " " & Request.Form(NewShit) & " " & Request.Form(DataTyp) & " NULL), "
End If
Next
SQLStmt = Left(SQLStmt, Len(SQLStmt) -2) & ""
Response.Write SQLStmt
On error resume next
Conn.Execute(SQLStmt)
Else
SQLStmt = "Select * From "& Tabell
Set RS = Conn.Execute(SQLStmt)
Set RSTypes = Conn.OpenSchema(22)
Dim arrData(14, 1)
cLoop = 0
Do Until RSTypes.EOF
If Not IsEmpty(RSTypes("Type_Name")) Then
arrData(cLoop, 0) = RSTypes("Type_Name")
arrData(cLoop, 1) = RSTypes("DATA_TYPE")
arrData(cLoop, 1) = RSTypes("Column_Size")
cLoop = cLoop + 1
End If
RSTypes.MoveNext
Loop
Set RSTypes = Nothing
%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<link REL="STYLESHEET" HREF="style/huvudstyle.css" TYPE="TEXT/CSS">
</HEAD>
<BODY BgColor="#FFFFFF" Text="#000000">
<table width="610" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="5"> </td>
<td width="600"><em class="b">Inställningar » Tabelladmin » <u><%=tabell%></u><br><br></em><em>
<a href="Tabell_AddField.asp?Tabell=<%= Tabell%>"><Img Src="Bilder/Create_Table.gif" Border=0> Skapa nytt fält</a>
<br><br>
</em>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td width="35%" bgcolor="#000000"><em class="mw">Fältnamn</em></td>
<td width="47%" bgcolor="#000000"><em class="mw">Datatyp</em></td>
<td width="9%" bgcolor="#000000"><p align="center"><em class="mw">Ändra</em></p></td>
<td width="9%" bgcolor="#000000"><p align="center"><em class="mw">Ta bort</em></p></td>
</tr>
<%
tcolor = "#cccccc"
For Each Item In RS.Fields
counter = counter + 1
If counter Mod 2 = 0 then
color = tcolor
Else
color = "#ffffff"
End If
If item.type = "202" Then
typ = "Text"
typ2 = "VarChar"
ElseIf item.type = "203" Then
typ = "PM"
typ2 = "LongText"
ElseIf item.type = "7" Then
typ = "Datum/Tid"
typ2 = "DateTime"
ElseIf item.type = "3" Then
typ = "Tal"
typ2 = "Long"
End If
%>
<tr>
<td width="35%" bgcolor="<%=color%>" valign="top"><em><%=item.name%><%%></em></td>
<td width="47%" bgcolor="<%=color%>" valign="top"><em><%=typ%></em></td>
<td width="9%" bgcolor="<%=color%>" valign="top"><p align="center"><a href="tabell_editfield.asp?field=<%=item.name%>&datatyp=<%=typ2%>&null=NULL&tabell=<%=tabell%>"><img src="../images/icons/redigera.gif" width="17" height="16" border="0"></a></td>
<td width="9%" bgcolor="<%=color%>" valign="top"><p align="center"><em><a href="#" OnClick="DeletePost('<%= Tabell%>', '<%= Item.Name%>')"><img src="../images/icons/radera.gif" alt="Ta bort" width="14" height="16" border="0"></a></em></td>
</tr>
<% Next %>
</Table>
<br><br>
<a href="Tabell.asp?Tabell=<%= Tabell%>"><<-- Återgå</a>
</b></font>
<Script Language="Javascript">
function DeletePost(Tabell, Field)
{
if(confirm("Är du säker på att du vill ta bort fältet \"" + Field + "\"?"))
location.href = "Tabell_DeleteField.asp?Tabell=" + Tabell + "&Field=" + Field;
}
</Script>
<%
Conn.Close
Set Conn = Nothing
Set RS = Nothing
%>
<% End If %>
B brw Medlem sedan juli 2000 619 inlägg Hur ska jag veta vilken post som jag får information från när jag använder denna: Set RSTypes = Conn.OpenSchema(22)
i ovanstående kod alltså?
L LarsG Medlem sedan dec. 2000 12 464 inlägg 22 innebär att du hämtar generell information om vilka typer som databasen kan hantera. Det har inget med en specifik tabell att göra.
Det är 4 du bör använda
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/oledb/htm/oledbcolumns_rowset.asp
B brw Medlem sedan juli 2000 619 inlägg :q
jag vet inte riktigt hur jag ska använda den i det här fallet men får du tid över.. får du gärna ge mig ett kodförslag utifrån den kod som jag bifogade så ser du jag tänkt... det ska alltså läggas till en kolumn i tabellen som heter size och på dom som är text eller varchar ska det då visas max teckenlängd på den kolumnen..
:h