2016-06-10 55 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()); 
     } 
    } 

}