04-03-2006, 16:30
|
|
|
|
חבר מתאריך: 13.05.02
הודעות: 8,468
|
|
בעיית כתיבה ל- DB - זקוק לעזרה דחופה
שלום לכולם
אני בונה פרוייקט קטן שצריך, בין היתר, לכתוב ל-DB דרך קובץ aspx.
מטעמי נוחות בחרתי ב-MS-Access (צריך משהו ממש קטן).
כבר הצלחתי ליצור גירסה שעבדה לי ב-localhost (עם עברית והכל),
אבל אחרי שהעלתי את הכל ל-Brinkster, כולל החלוקה לספריות לפי הדרישה שלהם (מאגר נתונים בספריית "db"),
גיליתי שפיתחתי בצורה שכבר לא עובדת (עם פקודת Set).
שיניתי הרבה דברים ואני די תקוע.
אני מפרסם פה את הקוד. אשמח מאד אם מישהו אחר יוכל להסתכל עליו ולנסות לזהות בעיות.
עריכה:
שכחתי לציין שאני צריך להגיש את הפרוייקט הזה מחר (כמעט הכל מוכן חוץ מזה), כך שזה די דחוף לשעות הקרובות.
כמו כן, מדובר על כתיבה לשתי טבלאות, כאשר המפתח של הראשונה הוא שדה מספור אוטומטי,
לכן בשאילתא השנייה אני שולף את המספר האחרון שנוצר, בכדי לכתוב אותו לטבלה השנייה (בשאילתא השלישית ), כי גם בה הוא חלק מהמפתח.
קוד:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001" Debug="true"%>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>הכנסת סקר למאגר</title>
</head>
<body>
<%
'Create DB connection
Dim pathStr as String = Server.MapPath("\evyatarbh\db\surveysData.mdb")
Dim conStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & pathStr
'Dim conStr As String = "server=" & pathStr & "\;Trusted_Connection=yes;database=data.mdb"
Dim oConn As New OleDbConnection(conStr)
'Dim oConn As New SqlConnection(conStr)
oConn.Open()
'Get data variables from the requesting form
dim Title as string = Request.Form("surveyTitle")
dim Inst as string = Request.Form("instructions")
dim Q1 as string = Request.Form("q1")
dim A1 as string = Request.Form("ans1")
dim A2 as string = Request.Form("ans2")
dim A3 as string = Request.Form("ans3")
dim A4 as string = Request.Form("ans4")
'First SQL query - Create survey header
dim mySQL as string = "INSERT INTO Survey(SurveyName,[Date],Introduction ) " & _
"VALUES ('" & Title & "', #" & now() & "#, '" & Inst & "');"
dim command = New OleDbCommand(mySQL,oConn)
command.ExecuteNonQuery()
'Second SQL query - Get new survey number
mySQL = "SELECT max(Survey.SurveyNo) as SN FROM Survey;"
command.CommandText = mySQL
dim dbread = command.ExecuteReader()
dim SN
do while (dbread.Read())
SN=dbread.GetInt32(0)
loop
'Third SQL query - Create survey question
mySQL = "INSERT INTO Question(SurveyNo,QuestionNo,Description,AnswerNo1 ,AnswerNo2,AnswerNo3,AnswerNo4 ) " & _
"VALUES (" & SN & ",1,'" & Q1 & "','" & A1 & "','" & A2 & "','" & A3 & "','" & A4 & "');"
command.CommandText = mySQL
command.ExecuteNonQuery()
'Close DB connection and redirect to main menu
dbread.close
oConn.close
Response.redirect("index.html?afterNew=true")
%>
</body>
</html>
תודה מראש.
_____________________________________
נערך לאחרונה ע"י EvyatarBH בתאריך 04-03-2006 בשעה 16:49.
|