C#读取SQL数据库,并将读取的数据上传至服务简易Demo
1.使用的NuGet包
2.IRepository 接口
{ IEnumerable<Package> GetAllPackages(); void DeleteByCodePackages(string code); // void InitRepository(); }
3.SimpleRepository类
public class SimpleRepository : IRepository { public IDbConnection PicInfoConnection => new SQLiteConnection($"datasource=\"{AppDomain.CurrentDomain.BaseDirectory}\\PicUp\\picinfos.db\";cache_size=10000"); /// <summary> /// 获取所有数据 /// </summary> /// <returns></returns> public IEnumerable<Package> GetAllPackages() { return PicInfoConnection.Query<Package>("select * from packages order by id desc"); } //public void InitRepository() //{ // try // { // if (!File.Exists(AppDomain.CurrentDomain.BaseDirectory + "picinfos.db")) // PicInfoConnection.Execute(createPicInfoSQL); // } // catch (Exception ex) // { // } //} //private string createPicInfoSQL = // "-- ----------------------------\n-- Table structure for picinfos\n-- ----------------------------\nDROP TABLE IF EXISTS \"picinfos\";\nCREATE TABLE \"picinfos\" (\n \"id\" INTEGER NOT NULL,\n \"barcode\" TEXT,\n \"pic_path\" TEXT,\n \"update_time\" text,\n \"mark\" integer,\n PRIMARY KEY (\"id\")\n);"; /// <summary> /// 清除指定条码的数据 /// </summary> /// <returns></returns> public void DeleteByCodePackages(string code) { string sqlStr = $"DELETE from packages WHERE barcode = '{code}'"; PicInfoConnection.Execute(sqlStr); } } public class Package { public int Id { get; set; } public string barcode { get; set; } public string pic_path { get; set; } public DateTime? update_time { get; set; } public string? mark { get; set; } }
4.App.xaml.cs
public partial class App : Application { public static Autofac.IContainer Container { get; set; } public App() { InitializeComponent(); BuildDependency(); } public static void BuildDependency() { var Builder = new ContainerBuilder(); Builder.RegisterType<picUp.SimpleRepository>().As<IRepository>().SingleInstance(); Container = Builder.Build(); // Container.Resolve<IRepository>().InitRepository(); //Container.Resolve<UploadJob>(); }
5.MainWindow.xaml
<Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Text="图片上传服务地址:" VerticalAlignment="Center" FontWeight="Bold" /> <TextBox x:Name="txtBaseUrl" Margin="3" Height="25" Width="350" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Left"/> <Button x:Name="btUpload" Click="btUpload_Click" Grid.Row="3" Style="{StaticResource ButtonPrimary}" Margin="10" Grid.Column="0" Content="上传"/> <Button x:Name="btClose" Click="btClose_Click" Grid.Row="3" Style="{StaticResource ButtonPrimary}" Margin="10" Grid.Column="1" Content="退出"/> </Grid>
6.MainWindow.xaml.cs
public partial class MainWindow : Window { private IRepository _repository; public MainWindow( ) { InitializeComponent(); _repository = App.Container.Resolve<IRepository>(); var picinfo= _repository.GetAllPackages(); IEnumerator enumerator= picinfo.GetEnumerator(); var piclist=new List<Package>(); while (enumerator.MoveNext()) { Package item = (Package)enumerator.Current; piclist.Add(item); } for (int i = 0; i < piclist.Count; i++) { string barcode = piclist[i].barcode; string picUrl = piclist[i].pic_path; // Upload(txtBaseUrl.Text, barcode, picUrl); MessageBox.Show(barcode); } } /// <summary> /// 例子 /// </summary> /// <param name="url"></param> /// <param name="barcode"></param> /// <param name="picUrl"></param> /// <returns></returns> private static bool Upload(string url, string barcode, string picUrl) { RestClient client = new RestClient(); client.Timeout = 5000; client.ReadWriteTimeout = 5000; client.BaseUrl = new Uri($"http://{url}/ydadl/public/common/commitScanInfo.do"); var request = new RestRequest(); request.Method = Method.POST; request.AddParameter("shipId", barcode, ParameterType.GetOrPost); request.AddParameter("weight", 0, ParameterType.GetOrPost); request.AddParameter("inductionId","", ParameterType.GetOrPost); if (true) { client.ExecutePostTaskAsync(request); Thread.Sleep(200); return true; } else { var result = client.Post(request); if (result.StatusCode != System.Net.HttpStatusCode.OK) { return false; } Thread.Sleep(200); } return true; } /// <summary> /// 上传图片 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btUpload_Click(object sender, RoutedEventArgs e) { _repository.DeleteByCodePackages("20003456"); } int i = 0; private void btClose_Click(object sender, RoutedEventArgs e) { btClose.IsEnabled= false; } }
7.Demo源码下载
https://files.cnblogs.com/files/king10086/picUp.zip?t=1702014247&download=true