Winform系统开发
一、读取是数据库中的数据,并导出csv格式文件
string str = "Server=" + PublicUse.ip + "," + PublicUse.port + ";DataBase=Pol;User ID=sa;Password=znjtlhyjsc527";
SqlConnection conn = new SqlConnection(str);
conn.Open();
string strSql = "select * from features";
SqlCommand cmd = new SqlCommand(strSql, conn);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds);
using (StreamWriter streamWriter = new StreamWriter(@"C:\Users\admin\Desktop\hbxt\20210723\Pol_Project\bin\Debug\1.csv", false, Encoding.Default))
{
if (ds.Tables[0].Rows.Count > 0)
{
foreach (DataRow row in ds.Tables[0].Rows)
{
StringBuilder str1 = new StringBuilder();
foreach (DataColumn col in ds.Tables[0].Columns)
{
//插入“,”自动会分割成不同的行。
str1.Append(row[col.Caption.ToString()].ToString() + ",");
}
streamWriter.WriteLine(str1.ToString());
}
}
streamWriter.Flush();
streamWriter.Close();
}
conn.Close();
二、获取dateTimePicker选择的时间
string time00 = dateTimePicker14.Value.ToString("yyyy-MM-dd HH");
string time01 = dateTimePicker15.Value.ToString("yyyy-MM-dd HH");
输出格式为:2022-11-21 00
三、数据库列名有中文,并且列名下是数字
想要根据监测时间下的数据来选择一段数据区间,可以用下面的方法(sqlserver中):
strSql1 = "select * from zjnd where [监测时间] >= " + "'" + time00 + "'" + "and [监测时间] <=" + "'" + time01 + "'";
特殊字符(本例指的是监测时间这个中文字符)用中括号括起来,time00是字符串,sql语句中要用单引号将字符串包含,也就是
[监测时间] = '2022-11-21 00'
四、SqlServer数据库导入csv格式文件
参考这篇文章(https://blog.csdn.net/qq_44657834/article/details/105798966)