如何检查Access数据库表中是否存在特定列 [英] How to check if specific column exists in an Access database table
问题描述
我想知道如何检查Access
数据库中特定表(例如myTable)中是否存在特定列(例如日期).
I want to know how to check if a specific column (e.g: date) exists in a specific table(e.g: myTable) in an Access
database.
我已阅读此答案提供一个查询,该查询导致另一个查询.
I've read this answer which offers a query which results in another query.
IF NOT EXISTS(SELECT *
FROM sys.columns
WHERE [name] = N'columnName'
AND [object_id] = OBJECT_ID(N'tableName'))
BEGIN
ALTER TABLE ADD COLUMN MYCOLUMN
END
但是我需要的是true/false
结果.
更新1
如何在C#应用程序中做到这一点?
How can I do that in my C# application?
也许使用SqlCommand.ExecuteScalar()
或其他方式?
推荐答案
感谢提供解决方案的每个人,收集了一些答案,我提出了自己的解决方案版本.也许这不是最好的解决方案,但是至少我不需要额外的dll来添加引用或处理某些不支持Access的stored procedures
.
Thanks to everyone who offered a solution, gathering up some of the answers, I came up with my own version of solution. Maybe it's not the best solution around, but at least I don't need an extra dll to add to the references or deal with some stored procedures
Access won't support.
OleDbConnection con = new OleDbConnection("my database address");
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT TOP 1 date FROM myTable";
con.Open();
bool exists = true;
try
{
var x = cmd.ExecuteScalar();
}
catch (Exception e)
{
exists = false;
}
con.Close();
这篇关于如何检查Access数据库表中是否存在特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!