2016-06-10 53 views
-2

我想要發生的是顯示上傳的文件在網格視圖(工作正常),併發送相同的文件到SQL數據庫(工作正常)。文件上傳發送到數據庫,並立即顯示到gridview

我的主要任務是獲取當前登錄人員下的所有上傳文件,例如customerA登錄,僅顯示客戶A的上傳文件。

我已經設法獲取會話用戶並將其存儲在數據庫 enter image description here

具有在我的數據庫,我現在實現一個select語句。 (這裏是我的問題開始的地方),這裏是頁面加載代碼:

public static string cs = "Server=PAULO;Database=ShoppingCartDB;Integrated Security=true"; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Session["New"] != null) 
     { 
      if (!IsPostBack) 
      { 
       SqlConnection con = new SqlConnection(cs); 
       con.Open(); 

       string sql = "SELECT * FROM DepositSlip Where Username = '" + Session["New"] + "'"; 
       SqlDataAdapter da = new SqlDataAdapter(sql, con); 
       DataTable dt = new DataTable(); 
       da.Fill(dt); 

       Label2.Text += Session["New"].ToString(); 
       linkLogout.Visible = true; 
       linkOrderHistory.Visible = true; 
       Label2.Visible = true; 
       linkViewProfile.Visible = true; 

       string[] filePaths = Directory.GetFiles(Server.MapPath("~/Uploads/")); 
       List<ListItem> files = new List<ListItem>(); 
       foreach (string filePath in filePaths) 
       { 
        files.Add(new ListItem(Path.GetFileName(filePath), filePath)); 
       } 
       GridView1.DataSource= files; 
       GridView1.DataBind(); 
      } 
     } 
    } 

據我所知,它裝載了從目錄路徑的文件,但我不知道是如何實現顯示上傳的文件從基於會話用戶的數據庫中獲取。

的問題是不管它顯示了誰登錄了相同的數據。

對此有什麼技巧?

回答

0

我把它通過消除工作:

Response.Redirect(Request.Url.AbsoluteUri); 
0

嘗試這種方式。

private void button1_Click(object sender, EventArgs e) 

{ 
    SqlConnection con = new System.Data.SqlClient.SqlConnection(); 
    con = new System.Data.SqlClient.SqlConnection(); 
    con.ConnectionString = "Server='server_name';Database='database_name';Trusted_Connection=True;"; 
    con.Open(); 
    SqlDataAdapter da = new SqlDataAdapter(); 

    for (int i = 0; i <= dataGridView1.Rows.Count - 2; i++) 
    { 

     String insertData = "INSERT INTO Import_List(Fname, Lname, Age) VALUES (@Fname, @Lname, @Age)"; 
     SqlCommand cmd = new SqlCommand(insertData, con); 
     cmd.Parameters.AddWithValue("@Fname", dataGridView1.Rows[i].Cells[0].Value); 
     cmd.Parameters.AddWithValue("@Lname", dataGridView1.Rows[i].Cells[1].Value); 
     cmd.Parameters.AddWithValue("@Age", dataGridView1.Rows[i].Cells[2].Value); 
     da.InsertCommand = cmd; 
     cmd.ExecuteNonQuery(); 
    } 

    con.Close(); 
} 

這應該也適用。

private void button1_Click(object sender, EventArgs e) 
{ 

    //SqlConnection connection = new SqlConnection("Data Source='server_name';Initial Catalog='database_name';Trusted_Connection=True;"); 
    DataTable dt = (DataTable)dataGridView1.DataSource; 
    string connection = "Data Source=Excel-PC;Initial Catalog=Northwind.MDF;Trusted_Connection=True;"; 
    using (var conn = new SqlConnection(connection)) 
    { 
     conn.Open(); 
     using (SqlBulkCopy bulkCopy = new SqlBulkCopy(conn)) 
     { 
      bulkCopy.ColumnMappings.Add(0, "Fname"); 
      bulkCopy.ColumnMappings.Add(1, "Lname"); 
      bulkCopy.ColumnMappings.Add(2, "Age"); 

      bulkCopy.BatchSize = 10000; 
      bulkCopy.DestinationTableName = "Import_List"; 
      bulkCopy.WriteToServer(dt.CreateDataReader()); 
     } 
    } 

}