【C#图解教程 第十二章 数组】教程文章相关的互联网学习教程文章

c# 数组【代码】

定义数组1 int[] test1; //定义一个新数组 2 test1 = new int[10]; //定义数组长度(此时所有元素都是0) 3 4 int[] test2 = new int[] { 1, 2, 3, 4, 5, 6 }; //也可以直接定义元素 5 6 int[,] test3 = new int[3, 4]; //定义多维数组 7 8 int[][] test4 = new int[9][]; //定义交错数组1 int[] intArry = Array.CreateInstance(typeof(int), 10) as int[]; //(类型,长度) 数组之间的拷贝 1 //(原数组,原数组开始拷贝索引...

如何在C#中从SQL设置数据动态数组【代码】

如何在C#中从SQL Server设置数据. 我班的学生:public class student {public int StudentID { get; set; }public string FirstName { get; set; }public string LastName { get; set; } }我想从SQL设置数据SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "Select * From Student"; cmd.Connection = con; Da...

c#-使用Code-First MVC5 EF6在SQL表中存储DateTime属性而不是字节数组【代码】

当我尝试在MVC5中使用EF6初始化数据库时,出现错误:属性“ Timestamp”不是字节数组.只能为字节数组属性配置IsRowVersion.有没有一种方法可以使用FluentAPI覆盖IsRowVersion,还是有另一种方法可以使用MVC5 EF6存储DateTime,或者这仅仅是使用Timestamp数据注释的结果?我更喜欢存储为DateTime而不是字节数组.只是为了可视化,模型看起来像这样:public class UserProfile : IdentityUser{//ctorpublic UserProfile(){Random rnd = ne...

C#如何将数组插入对象mongodb【代码】

目前我正在使用C#开发一个应用程序,我想在MongoDB集合中添加一些数据.我正在尝试将一个数组添加到Employee对象,但我正在努力让它正常工作. 当我查看其他帖子时,我遇到了使用BsonDocument的语法,如下所示:var document = new BsonDocument { { "author", "joe" }, { "comments", new BsonArray {new BsonDocument { { "author", "jim" }, { "comment", "I disagree" } },new BsonDocument { { "author", "nancy" }, { "comment", "...

从C#将字节数组插入SQL Server以及如何检索它【代码】

我正在尝试将此字节数组插入SQL Server数据库,列数据类型为varbinary,这是我在C#中的代码SqlParameter param = new SqlParameter("@buffer", SqlDbType.VarBinary, 8000); param.Value = buffer;string _query = "INSERT INTO [dbo].[Files] (FileID, Data, Length) VALUES ('" + uID + "','" + buffer + "','" + buffer.Length + "')";using (SqlCommand comm = new SqlCommand(_query, conn)) {comm.Parameters.Add(param);try{c...

使用C#MongoDB驱动程序的嵌套数组$pull查询【代码】

我按照预期在mongo shell上运行以下查询.db.getCollection('personnels').update({_id: ObjectId("55f6728b9d73a15807885de8"), "Devices._id":ObjectId("55fa5f7ac9e7863a3836e331")}, {$pull:{ "Devices.$.DeviceCloudFolders": { "CloudFolderId": ObjectId("5615124b06275f072040c4f1")}}} );这是我的文档结构:{"_id" : ObjectId("55f6728b9d73a15807885de8"),"FirstName" : "Tolga","Devices" : [ {"_id" : ObjectId("55fa5f...

当我们将其传递给函数作为参数时,此数组将在C#中占用多少内存【代码】

我在C#中使用数组,如下所示:class abc {xyz x = new xyz(); // some other classpublic double [] arr = new double [100]; // Allocated 400 bytes // for this array.// 100 is imaginary size // shown here for simplicity.// Actual size will depend on input.private void something() {x.funA(arr);} }在上面的abc类中,数组消耗400个字节.现在,我已将此数组作为某些参数传递给类xyz中的参数. 我的问题是,是否将在类xyz中...

c#-在内存中存储数组的最佳实践【代码】

因此,我们有一个WCF服务,可以为客户端提供一些对象.我们的wcf服务从数据访问层(DAL)获取数据. DAL从API获取数据,并将其格式化为我们可以使用的对象.对于大多数对象来说,这很好. 但是我们还需要API中的对象列表,并且这些对象不会更改.针对API的查询需要15到20秒.长时间对同一数据多次运行此查询的方法.因此,我们想将列表存储在内存中.因为DBA不是我们最好的朋友,所以SQLite或SQL CE也不是一个选择. 现在我们有了类似的东西,第一次加...

c# – 数组内存分配 – 分页

不确定Java,C#和C的答案是否相同,所以我对所有答案进行了分类.所有语言的答案都会很好. 我一直在思考的所有日子,如果我分配数组,所有单元格将在一个连续的空间中.因此,如果系统中没有足够的内存,则会出现内存不足异常. 没事,我说的是什么?或者是否有可能,分配的数组将被分页?解决方法:C阵列是连续的,这意味着存储器具有连续的地址,即它在虚拟地址空间中是连续的.它在物理地址空间中不需要是连续的,因为现代处理器(或它们的存储器...

C# 字符串string和内存流MemoryStream及比特数组byte[]之间相互转换【代码】

定义string变量为str,内存流变量为ms,比特数组为bt 1.字符串转比特数组 复制代码 代码如下: (1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串"); (2)byte[] bt=Convert.FromBase64String("字符串"); 2.字符串转流 复制代码 代码如下: (1)MemoryStream ms=new MemoryStream(System.Text.Encoding.Default.GetBytes("字符串")); (2)MemoryStream ms=new MemoryStream(Convert.FromBase64String("字符串")); 3.流...

c# – 将Word文档转换为内存中的pdf字节数组【代码】

我需要打开一个Microsoft Word文档,替换一些文本然后转换为pdf字节数组.我已经创建了代码来执行此操作,但它涉及将pdf保存到磁盘并将字节读回内存.我想避免在磁盘上写任何东西,因为我不需要保存文件. 以下是我到目前为止所做的代码……using System.IO; using Microsoft.Office.Interop.Word;public byte[] ConvertWordToPdfArray(string fileName, string newText) {// Temporary path to save pdfstring pdfName = fileName.Subst...

C#使用LINQ Query将记录与进程数组的结果进行比较【代码】

我编写了以下代码来比较一列的DataSet记录(即)记录.我得到以下例外:ex:” Index was outside the bounds of the array.”public void GetRunningTask(){// Process[] lstprocess = Process.GetProcesses();conn=new SqlConnection("Data Source=.; Initial Catalog='TTES'; Integrated Security=SSPI;");da=new SqlDataAdapter("Select AppName from LRNSetting", conn);ds=new DataSet();da.Fill(ds,"LRNSetting");// Process[] ...

c#中字节数组byte[]、图片image、流stream,字符串string、内存流MemoryStream、文件file,之间的转换

public class FileStreamByteTool{/*********字节数组byte[]与图片image之间的转化**********///字节数组转换成图片public static Image byte2img(byte[] buffer){MemoryStream ms = new MemoryStream(buffer);ms.Position = 0;Image img = Image.FromStream(ms);ms.Close();return img;}//图片转化为字节数组public static byte[] byte2img(Bitmap Bit){byte[] back = null;MemoryStream ms = new MemoryStream();Bit.Save(ms, Sy...