用C#中的if语句检查表是否存在?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16146473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if table exists with if statement in C#?
提问by Praise
I try to put up an if statement to check if a table is already created. I only want to create one table, but as it is now I create a table every time I click the button to store the info. Any suggestions?
我尝试提出一个 if 语句来检查是否已经创建了一个表。我只想创建一个表,但现在我每次单击按钮来存储信息时都会创建一个表。有什么建议?
DataTable dt;
private void InitDataTable()
{
if () {
}
dt = new DataTable();
DataSet ds = new DataSet();
ds.ReadXml("gjesteInfo.xml");
ds.Tables.Add(dt);
DataColumn dc1 = new DataColumn("Fullt navn");
DataColumn dc2 = new DataColumn("Start dato");
DataColumn dc3 = new DataColumn("Antall dager");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Rows.Add(gjestenavnInput.Text, datoInnsjekk.Text, antallDager.Text);
ds.Merge(dt);
ds.WriteXml("gjesteInfo.xml");
}
private void lagre_Click(object sender, EventArgs e)
{
InitDataTable();
gjesterutenrom.Items.Add(gjestenavnInput.Text);
gjestenavnInput.Text = "";
datoInnsjekk.Text = "";
antallDager.Text = "";
DataSet onClick = new DataSet();
onClick.ReadXml("gjesteInfo.xml");
lagredeGjester.DataSource = onClick.Tables[0];
}
I try to get out the info stored in the XLM with a DataGridView named lagredeGjester as seen over.
我尝试使用名为 lagredeGjester 的 DataGridView 获取存储在 XLM 中的信息,如所见。
UPDATED QUESTION :
更新的问题:
Now I wrote the code like this :
现在我写了这样的代码:
DataTable dt;
DataSet ds = new DataSet();
private void InitDataTable()
{
if( ds.Tables.Contains("Gjester") )
{
dt.Rows.Add(gjestenavnInput.Text, datoInnsjekk.Text, antallDager.Text);
ds.Merge(dt);
ds.WriteXml("gjesteInfo.xml");
}
else {
dt = new DataTable("Gjester");
ds.ReadXml("gjesteInfo.xml");
ds.Tables.Add(dt);
DataColumn dc1 = new DataColumn("Fullt navn");
DataColumn dc2 = new DataColumn("Start dato");
DataColumn dc3 = new DataColumn("Antall dager");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
dt.Rows.Add(gjestenavnInput.Text, datoInnsjekk.Text, antallDager.Text);
ds.Merge(dt);
ds.WriteXml("gjesteInfo.xml");
}
}
On my first run I entered two different infomations and pressed my button. Both info got in the same table as I wanted it to. But I can't seem to write my if statement correctly. When I run the code above it works with an empty XML (with no tables), but as long as "Gjester" table is created it says "A DataTable named 'Gjester' already belongs to this DataSet." But isn't this what my if statement should prevent? As I wrote it now, should it not just add the info and not try to create a new table?
在我第一次运行时,我输入了两个不同的信息并按下了我的按钮。两个信息都按照我的意愿出现在同一张表中。但是我似乎无法正确编写 if 语句。当我运行上面的代码时,它使用一个空的 XML(没有表),但只要创建了“Gjester”表,它就会显示“名为 'Gjester' 的数据表已经属于这个数据集。” 但这不是我的 if 语句应该防止的吗?正如我现在写的那样,它不应该只是添加信息而不是尝试创建一个新表吗?
回答by Freelancer
Can Check via:
可以通过以下方式检查:
if(ds.Tables.Contains("tablename"))
OR
或者
if(dt.Rows.Count == 0)
OR
或者
int flag=0;
try
{
if(ds.Tables["tablename"].Rows.Count>0)
{
// execute something
}
}
catch(Exception ex)
{
flag=1;
}
if(flag==1)
{
messagebox.show("Table does not exists");
}
回答by Hossein Narimani Rad
A possible solution would be to define the DataSetout of the function. Then check number of tables in the dataset.
一个可能的解决方案是定义DataSet函数外。然后检查数据集中的表数。
DataSet ds = new DataSet();
private void InitDataTable()
{
DataTable dt;
if(ds.Tables.Count > 0 )
{
dt = ds.Tables[0];
}
dt = new DataTable();
//your code
}
回答by Charles Bretana
You can also Extend DataSet to add a method FetchOrCreate()
您还可以扩展 DataSet 以添加方法 FetchOrCreate()
public static DataTable FetchOrCreate(this DataSet ds, string tableName)
{
if (ds.Tables.Contains(tableName))
return ds.Tables[tableName];
// -------------------------------
var dt = new Datatable(tableName);
ds.Tables.Add(dt);
return dt;
}

