琐碎--选择文件夹(路径)+生产txt格式的log+数据库操作方式

记录日常工作常用到的一些方法:

1 选择文件操作,并将文件的路径记录下来:

OpenFileDialog ofd = new OpenFileDialog();
            ofd.Multiselect = false;
            ofd.Title = "请选择文件";
            ofd.Filter = "(*.*)|*.*";
            if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tb1.Text = ofd.FileName;//文件的路径
            }

 

2 选择文件夹的操作,并将文件夹的路径记录下来:

FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.Description = "请选择文件夹";
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string str = fbd.SelectedPath;//文件夹的路径
            }

 

3 、生成txt格式的log:

public void WriteLog(string txt)
        {
            try
            {
                txt = DateTime.Now.ToString() + " " + txt;

                string str = System.IO.Directory.GetCurrentDirectory();
                string firstStr = str.Substring(0, 1);
                //生成文件夹
                string filesPath = firstStr + ":/倒计时日志";
                if (!Directory.Exists(filesPath))
                {
                    Directory.CreateDirectory(filesPath);
                }

                string filePath = filesPath + "/" + DateTime.Now.ToLongDateString()+ ".txt";
                System.IO.FileStream fs = new System.IO.FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Write | FileShare.ReadWrite | FileShare.Read);
                fs.Close();
                System.IO.StreamWriter sw = new StreamWriter(@filePath, true, Encoding.Unicode);
                sw.WriteLine(txt);
                sw.Close();
            }
            catch (Exception ex)
            {
            }
        }

 

4 操作数据库常用方法:

数据库连接:static string connectionString = "Data Source=192.168.100.46;Initial Catalog=ExamDB;User ID=sa;Password=123";//连接数据库语句

查询数据库,返回一个数据集:

public static DataSet Query(string sqlString)
        {
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                DataSet ds = new DataSet();
                try
                {
                    sqlConnection.Open();
                    SqlDataAdapter sqlDataApater = new SqlDataAdapter(sqlString, sqlConnection);
                    sqlDataApater.Fill(ds, "ds");
                }
                catch (SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds;
            }
        }

 

public static DataSet Query2(string sqlString)
        {
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
                {
                    DataSet ds = new DataSet();
                    try
                    {
                        sqlConnection.Open();                       
                        sqlCommand.CommandText = sqlString;
                        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
                        sqlDataAdapter.Fill(ds);
                    }
                    catch (SqlException ex)
                    {
                        throw new Exception(ex.Message);
                    }
                    return ds;
                }
            }
        }

 

执行数据库操作,返回影响的行数:

public static int ExecuteSql(string sqlString)
        {
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                using (SqlCommand sqlCommand = new SqlCommand(sqlString, sqlConnection))
                {
                    try
                    {
                        sqlConnection.Open();
                        int rows = sqlCommand.ExecuteNonQuery();
                        return rows;
                    }
                    catch (SqlException ex)
                    {
                        sqlConnection.Close();
                        throw new Exception(ex.Message);
                    }
                }
            }
        }

 

 

5、生成文件夹

string str = System.IO.Directory.GetCurrentDirectory();
                string firstStr = str.Substring(0, 1);
                string filesPath = firstStr + ":/学习";
                if (!Directory.Exists(filesPath))
                {
                    Directory.CreateDirectory(filesPath);
                }

 

 

你可能感兴趣的:(数据库)