NuGet 安裝SqlSugar
1.Model文件下新建 DbContext 類
public class DbContext { public DbContext() { Db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "server=localhost;uid=root;pwd=woshishui;database=test", DbType = DbType.MySql, InitKeyType = InitKeyType.Attribute,//從特性讀取主鍵和自增列信息 IsAutoCloseConnection = true,//開啟自動釋放模式和EF原理一樣我就不多解釋了 }); //調式代碼 用來打印SQL Db.Aop.OnLogExecuting = (sql, pars) => { Console.WriteLine(sql + " " + Db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); Console.WriteLine(); }; } //注意:不能寫成靜態的,不能寫成靜態的 public SqlSugarClient Db;//用來處理事務多表查詢和複雜的操作 public SimpleClient<uploading> uploadingdb { get { return new SimpleClient<uploading>(Db); } }//用來處理Student表的常用操作 }
2.建uploading實體類
[SugarTable("uploading")] public class uploading { //指定主鍵和自增列,當然數據庫中也要設置主鍵和自增列才會有效 [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int id { get; set; } public string name { get; set; } public string path { get; set; } }
3.Manager文件下建UploadingManager
class UploadingManager : DbContext { public ListQuery() { try { Listdata = Db.Queryable() .Select(f => new uploading { name = f.name, path = f.path }) .ToList(); return data; } catch (Exception e) { Console.WriteLine(e); throw; } } public List<string> GetName(string name) { List<string> data = Db.Queryable<uploading>() .Where(w=>w.name== name) .Select(f => f.path) .ToList(); return data; } }
窗體加載Form1_Load
1.讀取到數據庫字段name並賦值
private void Form1_Load(object sender, EventArgs e) { Listdata = uploading.Query(); foreach (var data1 in data) { comboBox1.Items.Add(data1.name); } comboBox1.SelectedIndex = 0; }
2.comboBox事件觸發條件查詢到上傳的path
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { Listdata = uploading.GetName(comboBox1.Text); for (int i = 0; i < data.Count; i++) { textBox1.Text = data[0]; } }
3.上傳事件觸發
private void Button1_Click(object sender, EventArgs e) { string path = textBox1.Text; CopyDirs(textBox3.Text, path); }
private void CopyDirs(string srcPath, string aimPath) { try { // 檢查目標目錄是否以目錄分割字符結束如果不是則添加 if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar) { aimPath += System.IO.Path.DirectorySeparatorChar; } // 判斷目標目錄是否存在如果不存在則新建 if (!System.IO.Directory.Exists(aimPath)) { System.IO.Directory.CreateDirectory(aimPath); } // 得到源目錄的文件列表,該裡面是包含文件以及目錄路徑的一個數組 // 如果你指向copy目標文件下面的文件而不包含目錄請使用下面的方法 // string[] fileList = Directory.GetFiles(srcPath); string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath); // 遍歷所有的文件和目錄 foreach (string file in fileList) { // 先當作目錄處理如果存在這個目錄就遞歸Copy該目錄下面的文件 if (System.IO.Directory.Exists(file)) { CopyDir(file, aimPath + System.IO.Path.GetFileName(file)); DisplaylistboxMsg("上傳成功"); } // 否則直接Copy文件 else { System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file), true); DisplaylistboxMsg("上傳成功"); } } } catch (Exception e) { DisplaylistboxMsg("上傳失敗" + e.Message); } }
4.下載事件觸發
private void Button2_Click(object sender, EventArgs e) { CopyDir(@"10.55.2.3mech_production_line_sharingTest" + textBox2.Text, textBox4.Text); } private void CopyDir(string srcPath, string aimPath) { // 檢查目標目錄是否以目錄分割字符結束如果不是則添加 if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar) { aimPath += System.IO.Path.DirectorySeparatorChar; } // 判斷目標目錄是否存在如果不存在則新建 if (!System.IO.Directory.Exists(aimPath)) { System.IO.Directory.CreateDirectory(aimPath); } // 得到源目錄的文件列表,該裡面是包含文件以及目錄路徑的一個數組 // 如果你指向copy目標文件下面的文件而不包含目錄請使用下面的方法 // string[] fileList = Directory.GetFiles(srcPath); string[] fileList = System.IO.Directory.GetFileSystemEntries(srcPath); // 遍歷所有的文件和目錄 foreach (string file in fileList) { // 先當作目錄處理如果存在這個目錄就遞歸Copy該目錄下面的文件 if (System.IO.Directory.Exists(file)) { CopyDir(file, aimPath + System.IO.Path.GetFileName(file)); DisplaylistboxMsg("下載成功"); } // 否則直接Copy文件 else { System.IO.File.Copy(file, aimPath + System.IO.Path.GetFileName(file), true); DisplaylistboxMsg("下載成功"); } } }
[bom485332 ] c# 實現文件上傳下載功能的實例代碼已經有277次圍觀