【问题标题】:file upload sent to database and immediately shown into gridview文件上传发送到数据库并立即显示到gridview
【发布时间】:2016-10-11 17:33:32
【问题描述】:

我想要发生的是在网格视图中显示上传的文件(工作正常)并将相同的文件发送到 sql 数据库(工作正常)。

我的主要任务是获取当前登录的人下的所有上传文件。例如客户A登录,只会显示客户A的上传文件。

我已经设法获取会话用户并将其存储在数据库中

在我的数据库中,我现在实现了一个选择语句。 (这是我的问题开始的地方)这是页面加载代码:

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();
            }
        }
    }

我知道它从目录路径加载文件,但我不知道如何实现以根据会话用户显示从数据库上传的文件。

问题在于无论谁登录,它都会显示相同的数据。

这方面有什么技巧吗?

【问题讨论】:

    标签: c# sql asp.net gridview file-upload


    【解决方案1】:

    我通过删除使其工作:

    Response.Redirect(Request.Url.AbsoluteUri);
    

    【讨论】:

      【解决方案2】:

      试试这个方法。

      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());
              }
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2016-11-20
        • 2015-09-20
        • 1970-01-01
        • 2020-01-10
        • 1970-01-01
        • 1970-01-01
        • 2011-07-14
        • 1970-01-01
        • 2016-08-19
        相关资源
        最近更新 更多