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");
我正在编写上面的代码来用列填充数据表,这里我从 txtCname 获取列名,我从 Combobox cmbDttype
选择该列的数据类型。
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.
txtDtname
携带该数据表的表名。
从最后一行开始,它将 Schema 写入 exampledt,但 xsd 文件中的现有数据表丢失,并出现新创建的数据表。
最佳答案
您应该向 typeof operator 提供类型名称 (而不是变量名)。您的 strdttype
具有字符串类型,因此正确的代码将是:
dtxsd.Columns.Add(txtCname.Text, typeof(string));
另一种选择——获取变量类型:
dtxsd.Columns.Add(txtCname.Text, strdttype.GetType());
关于c# - 是一个字段,但像类型一样使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20214211/