我有這樣的代碼:用於上傳頁面cs文件的檢索ID 2008表asp.net網站,然後插入ID到另一個表
部分:
protected void Page_Load(object sender, EventArgs e)
{
BtnImport1.Visible = false;
Cancel.Visible = false;
}
private string ErrorMessage(string input)
{
if (!string.IsNullOrEmpty(input))
return input;
BtnImport1.Visible = false;
return "No value entered!";
}
protected void btnUpload_Click(object sender, EventArgs e)
{
string strFileNameOnServer = fileUpload.PostedFile.FileName;
string fileExt =
System.IO.Path.GetExtension(fileUpload.FileName);
if (fileUpload.PostedFile != null && fileExt == ".csv")
{
try
{
fileUpload.PostedFile.SaveAs(Server.MapPath("~/uploads"));
Label1.Text = "File name: " +
fileUpload.PostedFile.FileName + "<br>" +
fileUpload.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
fileUpload.PostedFile.ContentType;
}
catch (Exception ex)
{
Label1.Text = "Error saving <b>" + strFileNameOnServer + "</b><br>. " + ex.Message;
}
BtnImport1.Visible = true;
Cancel.Visible = true;
fileUpload.Visible = false;
btnUpload.Visible = false;
}
else
{
Label1.Text = "Error - a file name must be specified/only csv files are allowed";
return;
}
}
protected void BtnImport1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=PSeminar;Integrated Security=true;Trusted_Connection=Yes;MultipleActiveResultSets=true");
StreamReader Sr = new StreamReader(Server.MapPath("~/uploads"));
string line;
while ((line = Sr.ReadLine()) != null)
{
String[] tokens = line.Split(',');
const string SQL = "INSERT INTO Attendance ([GuestID], [IC_No], [Grouping], [Remarks], [GuestName], [Class_Group], [Staff], [Attendance_Parents_Only], [Registration], [Event_ID]) VALUES (@GuestID, @IC_No, @Grouping, @Remarks, @GuestName, @Class_Group, @Staff, @Attendance_Parents_Only, @Registration, @Event_ID)";
SqlCommand cmd = new SqlCommand(SQL, con);
cmd.Parameters.AddWithValue("@GuestID", tokens[0]);
cmd.Parameters.AddWithValue("@IC_No", tokens[1]);
cmd.Parameters.AddWithValue("@Grouping", tokens[2]);
cmd.Parameters.AddWithValue("@Remarks", tokens[3]);
cmd.Parameters.AddWithValue("@GuestName", tokens[4]);
cmd.Parameters.AddWithValue("@Class_Group", tokens[5]);
cmd.Parameters.AddWithValue("@Staff", tokens[6]);
cmd.Parameters.AddWithValue("@Attendance_Parents_Only", tokens[7]);
cmd.Parameters.AddWithValue("@Registration", tokens[8]);
cmd.Parameters.AddWithValue("@EventID", tokens[9]);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
Response.Redirect("Main.aspx");
}
所以我想要從SQL Server 2008表中獲得名爲Event
的EventID
,然後將其插入到名爲Attendance
的另一個表中,如BtnImport1_Click
所述。表中有EventID
和EventName
列,名爲Event
。我在另一個.cs頁面查詢了ListBox中的EventName
字符串。查詢字符串存儲EventName
:
protected void up_Click(object sender, EventArgs e)
{
foreach (ListItem li in ListBox1.Items)
{
//if the item is selected in the listboxs
if (li.Selected)
{
Response.Redirect("Upload.aspx?ename=" + this.ListBox1.SelectedItem);
}
}
現在,我不知道如何將存儲EventName
與EventID
匹配,並與其他信息在BtnImport1_Click
還插入EventID
到Event
表一起。
請幫幫忙,我已經破解我的頭,因爲幾天已經=/
只需注意:您使用的數據庫稱爲** SQL Server 2008 ** - 「管理」這個東西叫做SQL Server Management Studio - 但這不是**數據庫產品本身 - 它是一個與數據庫一起工作的GUI前端。所以它是一個** SQL Server **表 - 不是一個SQL Server管理表 - 你在這裏處理...... –