从sql server到csv的文件转换 [英] file conversion from sql server to csv

查看:130
本文介绍了从sql server到csv的文件转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有人都好,

我正在将c#.net中的sql server数据库表转换为csv格式.我得到了将整个表转换为csv的解决方案.但是我的疑问是我可以在csv格式期间插入,更新,删除数据库中的值.下面是我发送的示例代码.请纠正我的疑问.

Hai to all,

I am converting the sql server database tables into csv format in c#.net.I get the solution that the whole table is converted into csv.But my doubt is Can i insert,update,delete the values in the database during the csv format.Below i had send the sample code.please rectify my doubt.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using iTextSharp.text.pdf;
using iTextSharp.text.rtf;
using iTextSharp.text.xml;
using iTextSharp.text.html;
using iTextSharp.text.factories;




namespace WebApplication6
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strQuery = "select ID,NAME,AGE,PLACEOFBIRTH from RAJ";
            SqlCommand cmd = new SqlCommand(strQuery);
            DataTable dt = GetData(cmd);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        private DataTable GetData(SqlCommand cmd)
        {
            DataTable dt = new DataTable();
            String strConnString = ("Data Source=MACFEESERVER;Initial Catalog=ePO_LENOVO;User ID=sa;Password=sa");
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            try
            {
                con.Open();
                sda.SelectCommand = cmd;
                sda.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
                sda.Dispose();
                con.Dispose();
            }
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            /* Verifies that the control is rendered */
        }

        protected void OnPaging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();  
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.csv");
            Response.Charset = "";
            Response.ContentType = "application/text";

            GridView1.AllowPaging = false;
            GridView1.DataBind();

            StringBuilder sb = new StringBuilder();
            for (int k = 0; k < GridView1.Columns.Count; k++)
            {
                //add separator
                sb.Append(GridView1.Columns[k].HeaderText + '','');
            }
            //append new line
            sb.Append("\r\n");
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                for (int k = 0; k < GridView1.Columns.Count; k++)
                {
                    //add separator
                    sb.Append(GridView1.Rows[i].Cells[k].Text + '','');
                }
                //append new line
                sb.Append("\r\n");
            }
            Response.Output.Write(sb.ToString());
            Response.Flush();
            Response.End();
        }

        

    }
}

推荐答案

您正在检索数据,而不是将其显示在网格中.它是用于创建CSV的网格数据.
当然,您可以执行任何 CRUD [
You are retrieving the data and than displaying it in the grid. It is the grid data which is being used to create a CSV.
Of course, you can do any CRUD [^]operation in database, but in CSV only those will be reflected which are done before you called the function GetData().


这篇关于从sql server到csv的文件转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆