我想将savefiledialogue称为函数中的字符串 [英] I want to call the savefiledialogue as string from the function

查看:52
本文介绍了我想将savefiledialogue称为函数中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我一直在开发一个人工制品(Windows表单应用程序),它将输入作为数据库名称,sql查询,sql ip,sql id和密码,并将获取记录并在网格视图中显示以及制作一个CSV文件相同。我面临的问题是关于文件路径。我有一个csv文件创建功能,其中我已将文件路径作为字符串参数传递。当我调用该函数时,它不应该被硬编码,而应该将文件保存在savefiledialogue指定的位置。请找到代码并告诉我解决方案。在代码中,文件路径是硬编码的。我想将savefiledialogue称为函数中的字符串。谢谢



 私人  void  button1_Click( object  sender,EventArgs e)
{
try

{

字符串 str = Server = + IPfield.Text + ; + database = + DBfield.Text + ; + UID = + IDfield.Text + ; + password = + pwdfield.Text;
/ * String tbl =Table =+ textBox6.Text; * /
// System.Console.WriteLine(str);


SqlConnection con = new SqlConnection(str);



String query = Queryfield.Text;
SqlCommand cmd = new SqlCommand(query,con);
SqlDataAdapter dataadapter = new SqlDataAdapter(query,con);
DataSet ds = new DataSet();
con.Open();
/ * dataadapter.Fill(ds,VMS); * /
dataadapter.Fill(ds,query);

MessageBox.Show( 与SQL Server连接);
MessageBox.Show( 已连接);

DataTable dt = ds.Tables [query];


SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = CSV | * .csv;
saveFileDialog1.Title = 保存CSV文件;
// saveFileDialog1.ShowDialog();
DialogResult dr = saveFileDialog1.ShowDialog ();

if (dr == DialogResult.OK)
{
string strFilePath = saveFileDialog1.FileName;
// 使用流保存文件。
StreamWriter sw = new StreamWriter(strFilePath, true );
}

// CreateCSVFile(dt,);
CreateCSVFile(dt, C:\\CSVDATA.csv);

MessageBox.Show( CSV生成);


con.Close();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = query;




}

catch (例外情况)

{

// MessageBox.Show(es.Message);
MessageBox.Show( 无法连接\ n请输入正确的凭据);




}

}
public < span class =code-keyword> void CreateCSVFile(DataTable dt, string strFilePath)
{

#region Grid Grid to CSV

// 创建将导出网格数据的CSV文件。
StreamWriter sw = new StreamWriter(strFilePath,< span class =code-keyword> false );
// 首先我们将编写标题。
// DataTable dt = m_dsProducts.Tables [0];
int iColCount = dt.Columns.Count;
for int i = 0 ; i < iColCount; i ++)
{
sw.Write(dt.Columns [i]);
if (i < iColCount - 1
{
sw.Write( ) ;
}
}
sw.Write(sw.NewLine);
// 现在写下所有行。
foreach (DataRow dr in dt.Rows)
{
for int i = 0 ; i < iColCount; i ++)
{
if (!Convert.IsDBNull(dr [i]))
{
sw.Write(dr [i] .ToString());
}
if (i < iColCount - 1
{
sw.Write( );
}
}
sw.Write(sw.NewLine);
}
sw.Close();

#endregion
}

解决方案

你能用这种方式纠正下面的代码吗?





 saveFileDialog1.Title =  保存CSV文件; 
// saveFileDialog1.ShowDialog();
DialogResult dr = saveFileDialog1.ShowDialog ();

if (dr == DialogResult.OK)
{
string strFilePath = saveFileDialog1.FileName;
// 使用流保存文件。
CreateCSVFile(dt,strFilePath);
// StreamWriter sw = new StreamWriter(strFilePath,true);
}


Hi I have been developing an artefact(Windows form application) which will take take inputs as database name,sql query,sql ip,sql id and password and will fetch the record and display in the grid view as well as make a CSV file for the same. The issue I am facing is regarding the file path. I have a function for csv file creation wherein I have passed the file path as string argument. When I call the function,it should not be hardcoded instead it should save the file in the location specified through savefiledialogue. please find the code and tell me the solution. In the Code,the file path is hardcoded. I want to call the savefiledialogue as string from the function. Thanks

private void button1_Click(object sender, EventArgs e)
        {
            try
 
            {

                String str = "Server="+IPfield.Text +";"+ "database="+DBfield.Text +";"+ "UID="+IDfield.Text +";"+ "password="+pwdfield.Text;
                /*String tbl = "Table=" + textBox6.Text;*/
                //System.Console.WriteLine(str);

               
                SqlConnection con = new SqlConnection(str);

                

                String query = Queryfield.Text;
                SqlCommand cmd = new SqlCommand(query, con);
                SqlDataAdapter dataadapter = new SqlDataAdapter(query, con);
                DataSet ds = new DataSet();
                con.Open();
                /*dataadapter.Fill(ds, "VMS");*/
                dataadapter.Fill(ds, query);

                MessageBox.Show("connect with SQL server");
                MessageBox.Show("Connected");

                DataTable dt = ds.Tables[query];
               
                
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.Filter = "CSV|*.csv";
                saveFileDialog1.Title = "Save a CSV File";
                //saveFileDialog1.ShowDialog();
                DialogResult dr = saveFileDialog1.ShowDialog();
                
                if (dr == DialogResult.OK)
                {
                    string strFilePath = saveFileDialog1.FileName;
                    //save file using stream.
                    StreamWriter sw = new StreamWriter(strFilePath, true);
                }
               
                //CreateCSVFile(dt, "");
                CreateCSVFile(dt ,"C:\\CSVDATA.csv");
                
                MessageBox.Show("CSV generated"); 
                                                                   
                                          
                con.Close();
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = query;
                
               

 
            }
 
            catch(Exception es)
 
            {
 
               // MessageBox.Show(es.Message);
                MessageBox.Show("Unable to Connect \nPlease Enter Correct Credentials");
               
 
 
 
            }
 
        }
        public void CreateCSVFile(DataTable dt, string strFilePath)
        {
            
            #region Export Grid to CSV
           
            // Create the CSV file to which grid data will be exported.
            StreamWriter sw = new StreamWriter(strFilePath,false);
            // First we will write the headers.
            //DataTable dt = m_dsProducts.Tables[0];
            int iColCount = dt.Columns.Count;
            for (int i = 0; i < iColCount; i++)
            {
                sw.Write(dt.Columns[i]);
                if (i < iColCount - 1)
                {
                    sw.Write(",");
                }
            }
            sw.Write(sw.NewLine);
            // Now write all the rows.
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    if (!Convert.IsDBNull(dr[i]))
                    {
                        sw.Write(dr[i].ToString());
                    }
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);
            }
            sw.Close();

            #endregion
        }

解决方案

can you correct your below line of code in this way...


saveFileDialog1.Title = "Save a CSV File";
//saveFileDialog1.ShowDialog();
DialogResult dr = saveFileDialog1.ShowDialog();

if (dr == DialogResult.OK)
{
    string strFilePath = saveFileDialog1.FileName;
    //save file using stream.
    CreateCSVFile(dt ,strFilePath);
    //StreamWriter sw = new StreamWriter(strFilePath, true);
}


这篇关于我想将savefiledialogue称为函数中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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