我需要检查记录是否保存到数据库中。如果它保存在数据库中打开另一个表单,否则显示一条消息,它不在数据库中。
如果记录不在数据库中,我会收到此错误Object reference not set to an instance of an object.
这是我的代码,请帮助我在此处找到错误:
string cmdStr = "Select NO from COM_LIST_EXPORT where NO = '" + txtNO.Text + "'";
SqlCommand cmd = new SqlCommand(cmdStr, CN);
int count = (int)cmd.ExecuteScalar();
if (count == Convert.ToInt32(txtNO.Text))
{
Archive_Sader dd = new Archive_Sader();
dd.Show();
}
else
{
MessageBox.Show("please save first");
}
Run Code Online (Sandbox Code Playgroud) 我不需要通过Activator createInstance使用创建新实例,为什么我需要它呢?我需要在哪些情况下使用Activator.CreateInstance()?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
namespace App.CreateInstance
{
class Program
{
static void Main(string[] args)
{
new MyCustomerManager().Save <MyCustomer>(new object[] {
1,
"xxx",
"yyyy" });
}
}
public class MyCustomerManager
{
public void Save<TModel>(object[] Vals)
{
Type calcType = typeof(TModel);
object instance = Activator.CreateInstance(calcType);
PropertyInfo[] ColumnNames = instance.GetType()
.GetProperties();
for (int i = 0; i < ColumnNames.Length; i++)
{
calcType.GetProperty(ColumnNames[i].Name,
BindingFlags.Instance
| BindingFlags.Public )
.SetValue(instance, Vals[i], null);
}
string result = "";
for …Run Code Online (Sandbox Code Playgroud) 我开始创建一些将触发异步操作的类,我希望客户端注册一个回调来接收一些结果.最后我达到了以下代码.这只是一个例子,我想知道是否有更好的方法来使用TaskFactory和Action<>, Func<>
这是客户端的基本示例:
Client client2 = new Client();
client2.GetClientList(ClientCallBack);
private static void ClientCallBack(List<Client> listado)
{
//Receive the list result and do some stuff in UI
}
Run Code Online (Sandbox Code Playgroud)
这是Client类的GetCLientList异步示例:
public void GetClientList(Action<List<Client>> Callback)
{
List<Client> listado=null;
Task.Factory.StartNew(() =>
{
listado = new List<Client>{
new Client{ apellidos="Landeras",nombre="Carlos",edad=25},
new Client{ apellidos="Lopez", nombre="Pepe", edad=22},
new Client{ apellidos="Estevez", nombre="Alberto", edad=28}
};
//Thread.Sleep to simulate some load
System.Threading.Thread.Sleep(4000);
}).ContinueWith((prevTask) =>
{
Callback(listado);
}
);
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法呢?我知道我可以Task从我的函数返回并continueWith在客户端注册,但我想将它包装在类中.
编辑
我发布了另一个例子.我试图制作 …
我是C#的新手.我有一个简短的表单和一个长形式的客户端代码.短格式是一些字母字符和一些数字字符(ABC12),而长格式总是15个字符长,alpha和数字部分之间的空格用零填充(ABC000000000012).我需要能够从短格式转换为长格式.下面的代码就是我如何使用它 - 这是最好的方法吗?
public string ExpandCode(string s)
{
// s = "ABC12"
int i = 0;
char c;
bool foundDigit = false;
string o = null;
while (foundDigit == false)
{
c = Convert.ToChar(s.Substring(i, 1));
if (Char.IsDigit(c))
{
foundDigit = true;
o = s.Substring(0, i) + new String('0', 15-s.Length) + s.Substring(i,s.Length-i);
}
i += 1;
}
return (o); //o = "ABC000000000012"
}
Run Code Online (Sandbox Code Playgroud) 有一些关于Async-await的事情让我神秘化,我想知道是否有人能向我解释一些事情:
请注意,我的查询是在阅读之后:http://blogs.msdn.com/b/ericlippert/archive/2010/10/29/asynchronous-programming-in-c-5-0-part-two-whence -await.aspx
所以Eric Lippert说
方法上的"async"修饰符并不意味着"此方法被自动调度为在工作线程上异步运行"
为什么我们需要在我们想要异步运行的方法上放置Async?即
private async Task<int> GetMeInt()
{
return 0;
}
private async void stuff()
{
var num = GetMeInt();
DoStuff();
int end = await num;
for (int i = 0; i < end; i++)
Console.WriteLine("blahblahblah");
}
Run Code Online (Sandbox Code Playgroud)
问题是我们不希望GetMeInt在内部实际执行任何异步操作.我们只是希望它同步运行,但我们希望它在被另一个方法调用时作为一个整体异步运行.将async仅放在Stuff()方法上并允许GetMeInt()在另一个线程上运行并稍后返回似乎更明智.
基本上我相信它会是这样的:
private int GetMeInt()
{
return 0;
}
private async void stuff()
{
int? num = null;
Thread t = new Thread(() => num = GetMeInt());
t.Start();
DoStuff();
t.Join();
for (int i = 0; i …Run Code Online (Sandbox Code Playgroud) 我需要在控制台应用程序中读取一个950mb的txt文件,而不需要获取System.OutOfMemoryException,具有以下结构:
"6152902100000017";20110701;20110701;53;"D";30359130;"NOTA DE DEBITO";"DEB.COMPRA BCO";0;;0;"6152902100000017";0;0;0;0;0;"902"
Run Code Online (Sandbox Code Playgroud)
我可以读取较小的文件,但更大的文件会抛出异常.有什么建议?
我一直收到这个错误:
"无法隐式地将类型'double'转换为'int'.存在显式转换(您是否错过了转换?)"
码:
Console.WriteLine("ISBN-Prüfziffer berechnen");
Console.WriteLine("=========================");
Console.WriteLine();
Console.Write("ISBN-Nummer ohne Prüfziffer: ");
string ISBNstring = Console.ReadLine();
int ISBN = Convert.ToInt32(ISBNstring);
int PZ;
int i;
double x = Math.Pow(3, (i + 1) % 2);
int y = (int)x;
for (i = 1; i <= 12; i++)
{
PZ = ((10-(PZ + ISBN * x) % 10) % 10);
}
Console.WriteLine(PZ);
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)
这是新代码:
Console.WriteLine("ISBN-Prüfziffer berechnen");
Console.WriteLine("=========================");
Console.WriteLine();
Console.Write("ISBN-Nummer ohne Prüfziffer: ");
string ISBNstring = Console.ReadLine();
long ISBN = Convert.ToInt32(ISBNstring);
long ISBN1 = (Int64)ISBN; …Run Code Online (Sandbox Code Playgroud) 为什么返回a的方法Task在返回它的实例时不会被执行.我认为这必须发生,因为await方法/委托会将它放入某个队列,然后执行生成的任务.
那么为什么这Task在调用时永远不会被执行Do()?
public void Do()
{
SomeTask().Wait()
}
public async Task SomeTask()
{
return new Task(() => { Console.WriteLine("Hello World!") });
}
Run Code Online (Sandbox Code Playgroud)
编辑
或者我需要await Task.Run(...)吗?
非常感谢你!
c# ×8
async-await ×2
asynchronous ×2
.net ×1
alphanumeric ×1
asp.net ×1
file ×1
sql ×1
task ×1
taskfactory ×1
text-files ×1