C# 是字段,但用作类型
我编写上述代码是为了用列填充数据表,这里我从txtCname获取列名,并从ComboboxC# 是字段,但用作类型,c#,C#,我编写上述代码是为了用列填充数据表,这里我从txtCname获取列名,并从Comboboxcmbdtype中选择该列的数据类型 DataTable dtcol = new DataTable(); string strdttype = cmbDttype.SelectedItem.ToString(); dtcol.TableName = txtDtname.Text; dtxsd.Columns.Add(txtCname.Text, typeof(strdttype)); dtcol.Writ
cmbdtype
中选择该列的数据类型
DataTable dtcol = new DataTable();
string strdttype = cmbDttype.SelectedItem.ToString();
dtcol.TableName = txtDtname.Text;
dtxsd.Columns.Add(txtCname.Text, typeof(strdttype));
dtcol.WriteXmlSchema("@D:\Example\exampledt.xsd");
txtDtname
携带该数据表的表名。从最后一行开始,它将把模式写入exampledt,但xsd文件中的现有datatable丢失,新创建的datatable出现 您应该向提供类型名(而不是变量名)。您的
strdttype
具有字符串类型,因此正确的代码为:
dtxsd.Columns.Add(txtCname.Text, typeof(strdttype)); //In this line I am getting error strdttype is a field but used is used like a type.
另一个选项-获取变量类型:
dtxsd.Columns.Add(txtCname.Text, typeof(string));
您应该向提供类型名称(而不是变量名称)。您的
strdttype
具有字符串类型,因此正确的代码为:
dtxsd.Columns.Add(txtCname.Text, typeof(strdttype)); //In this line I am getting error strdttype is a field but used is used like a type.
另一个选项-获取变量类型:
dtxsd.Columns.Add(txtCname.Text, typeof(string));
这里使用typeof是错误的。您需要在这里指定ClassName而不是对象。要获取对象的类型,请尝试第二行
dtxsd.Columns.Add(txtCname.Text, strdttype.GetType());
这里使用typeof是错误的。您需要在这里指定ClassName而不是对象。要获取对象的类型,请尝试第二行
dtxsd.Columns.Add(txtCname.Text, strdttype.GetType());
我在你的代码中发现了两个不同的错误。一个很明显,但另一个是有条件的
var tt = typeof(int);
dtxsd.Columns.Add(txtCname.Text, strdttype.GetType());
其中,组合框中的值应采用以下格式:
“System.Int32”、“System.String”、“System.Int64”。等等Nitin Joshi我在您的代码中发现了两个不同的错误。一个很明显,但另一个是有条件的
var tt = typeof(int);
dtxsd.Columns.Add(txtCname.Text, strdttype.GetType());
其中,组合框中的值应采用以下格式:
“System.Int32”、“System.String”、“System.Int64”。等等Nitin Joshi当我将该模式写入xsd以创建数据表时,已经存在的数据表正在删除始终新创建的数据表显示在xsd文件中。当我将该模式写入xsd以创建数据表时,已经存在的数据表正在删除始终新创建的数据表显示在xsd文件中。