我跟踪存储在RolesSQL Server数据库中的XML列(称为)中的XML .
<root>
<role>Alpha</role>
<role>Beta</role>
<role>Gamma</role>
</root>
Run Code Online (Sandbox Code Playgroud)
我想列出在其中具有特定角色的所有行.此角色由参数传递.
我在下面简单的c#方法中有一个简单的强制转换问题
using System;
using System.Data.SqlTypes;
...
private void method1() {
string s = "TestString";
object o = s;
SqlString t1 = (SqlString) s;
SqlString t2 = (SqlString) o;
}
...
Run Code Online (Sandbox Code Playgroud)
当直接从s(在t1的情况下)投射时,我没有得到任何错误但是当o我从中获得异常时:
指定演员表无效.
我有同样的问题转换object为所有类型System.Data.SqlTypes
我怎样才能将其中的object字符串强制转换为SqlString?
我有以下简单的类定义(Windows控制台应用程序):
样品按预期进行
using System;
namespace ConsoleApplication1 {
class Test: IDisposable {
string Text;
public Test(string str) {
Text = str;
}
public void print() {
Console.WriteLine("Text: " + Text);
}
public void Dispose() {
Text = "";
}
}
class Program {
static void Main(string[] args) {
string TeXT = "Data Source=localhost;Initial Catalog=master;";
using (Test test = new Test(TeXT)) {
test.print();
}
using (Test test = new Test(TeXT)) {
test.print();
}
Console.ReadKey();
}
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我传递string给一个IDisposable类 …