我需要通过Visual Studio 2010连接到Oracle DB(外部).但我不想在我的机器上安装Oracle.在我的项目中,我引用了:System.Data.OracleClient.但它没有满足需要.我有一个"Oracle SQL Developer IDE",我在其中运行针对oracle db的SQL查询.
到目前为止我有这个代码:
private static string GetConnectionString()
{
String connString = "host= serverName;database=myDatabase;uid=userName;pwd=passWord";
return connString;
}
private static void ConnectingToOracle()
{
string connectionString = GetConnectionString();
using (OracleConnection connection = new OracleConnection())
{
connection.ConnectionString = connectionString;
connection.Open();
Console.WriteLine("State: {0}", connection.State);
Console.WriteLine("ConnectionString: {0}",
connection.ConnectionString);
OracleCommand command = connection.CreateCommand();
string sql = "SELECT * FROM myTableName";
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
while (reader.Read())
{
string myField = (string)reader["MYFIELD"];
Console.WriteLine(myField);
}
} …Run Code Online (Sandbox Code Playgroud) 下面是一个示例代码,它创建一个模拟长时间运行进程的新任务.任务没有太多关于取消功能.我使用取消令牌来取消任务,代码对我来说很好.
CancellationTokenSource CTS= new CancellationTokenSource();
Task<Boolean> PTask = new Task<Boolean>(() =>
{
while (true)
{
if (!CTS.Token.IsCancellationRequested)
{
Thread.Sleep(5000);
}
else{Console.WriteLine("Thread Cancelled");break;}
}
return true;
}, CTS.Token, TaskCreationOptions.None);
PTask.Start();
Console.WriteLine("Hit Enter to cancel the Secondary thread you have started");
Console.ReadLine();
CTS.Cancel();
System.Console.WriteLine(PTask.Result);
}
}
Run Code Online (Sandbox Code Playgroud)
但是我无法理解的是将令牌参数(CTS.Token)传递给任务构造函数.传递参数的实际用途是什么,即使我没有将令牌传递给构造函数也可以实际取消任务.
下面是一个稍微修改过的版本,没有令牌参数.
CancellationTokenSource CTS= new CancellationTokenSource();
Task<Boolean> PTask = new Task<Boolean>(() =>
{
while (true)
{
if (!CTS.Token.IsCancellationRequested)
{
Thread.Sleep(5000);
}
else
{
Console.WriteLine("Thread Cancelled");
break;
}
};
Run Code Online (Sandbox Code Playgroud) 我已经创建了一个方法来检查XML文件中是否存在属性.如果它不存在则返回"False".它可以工作,但解析文件需要很长时间.它似乎读取每一行的整个文件.我错过了什么吗?我可以以某种方式使它更有效吗?
public static IEnumerable<RowData> getXML(string XMLpath)
{
XDocument xmlDoc = XDocument.Load("spec.xml");
var specs = from spec in xmlDoc.Descendants("spec")
select new RowData
{
number= (string)spec.Attribute("nbr"),
name= (string)spec.Attribute("name").Value,
code = (string)spec.Attribute("code").Value,
descr = (string)spec.Attribute("descr").Value,
countObject = checkXMLcount(spec),
return specs;
}
public static string checkXMLcount(XElement x)
{
Console.WriteLine(x.Attribute("nbr").Value);
Console.ReadLine();
try
{
if (x.Attribute("mep_count").Value == null)
{
return "False";
}
else
{
return x.Attribute("mep_count").Value;
}
}
catch
{
return "False";
}
}
Run Code Online (Sandbox Code Playgroud)
我测试用一个只返回和接收字符串的方法替换方法:
public static string checkXMLcount(string x)
{
Console.WriteLine(x);
Console.ReadLine();
return x;
} …Run Code Online (Sandbox Code Playgroud) 给定一个Parent对象列表,每个对象都有一个Child对象列表,我想找到与特定ID匹配的子对象.
public class Parent
{
public int ID { get; set; }
public List<Child> Children { get; set; }
}
public class Child
{
public int ID { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在我希望Child对象具有特定的ID:
List<Parent> parents = GetParents();
Child childWithId17 = ???
Run Code Online (Sandbox Code Playgroud)
我怎么能用Linq做到这一点?
我有一个csv文件包含有值的列,'\\\n'并且'\\\t'转义为新行和制表符.但是,我想将每一行拆分为字符串数组.
如何分开'\n'但不是'\\\n'?
我正在看Regex.Split是正确的方向吗?我试过Regex.Split(input, @"[^\\]\n");但结果似乎是正确的,但前面的一个字符总是丢失,据说是由[^ \]引起的.
我有两个班级,Sale和SaleDTO.
当我使用 automapper 映射这两个类的对象时,它将起作用。
但是,如果我这样做:
List<Sale> s = GetSalesFromDatabaseMethod();
List<SaleDTO> sa = Mapping.Map<List<Sale>, List<SaleDTO>>(s);
Run Code Online (Sandbox Code Playgroud)
sa会变成空的。难道我做错了什么?
该Map方法基本上是映射的快捷方式:
public static H Map<T, H>(T i) {
Mapper.CreateMap<T, H>();
return Mapper.Map<T, H>(i);
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有:的HTML页面<script scr="myJavaScriptFile.js"></script>,以及一个名为myJavaScriptFile.js的javascript文件(在同一个文件夹中).
我的问题:javascript不起作用,因为我认为文件未加载.用于检查元素的Google Chrome工具的"来源"标签显示我不是我的.js文件.
我有其他页面脚本运行良好,所以我不知道是什么问题.
c# ×6
.net ×2
linq ×2
automapper ×1
c#-4.0 ×1
html ×1
javascript ×1
linq-to-xml ×1
oracle ×1
split ×1
string ×1
xml ×1