我必须找到数据库(MS Access)中没有域约束的每一列,并为每一列计算当前数据的最小值和最大值,然后从我的程序中添加相应的约束。
例如,“Foo”列的最小值为 0,最大值为 100,我需要约束“Foo 在 0 和 100 之间”。
如何在 C# 中检查列是否具有此约束?
在访问中,我在“验证规则”中找到了此内容。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace Proiect
{
public partial class Form1 : Form
{
private OleDbConnection con = new OleDbConnection();
private new OleDbCommand cmd = new OleDbCommand();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dataSet1.Carti'
this.cartiTableAdapter.Fill(this.dataSet1.Carti);
cartiTableAdapter.Fill(dataSet1.Carti);
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.ReadOnly = true;
con.ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;"+
"DataSource=D:\..\BD.accdb";
cmd.Connection=con;
this.cartiTableAdapter.Fill(this.dataSet1.Carti);
chkC.Checked=false;
}
private void chkC_CheckedChanged(object sender, EventArgs e)
{
}
}
}
最佳答案
为了检查应用于您的表的约束,您希望使用 OleDbSchemaGuid.Check_Constraints Field 。如何使用它实际上与您使用 OleDbSchemaGuid.Tables
的方式略有不同。
为了帮助您解决此问题,我为您编写了一个小控制台应用程序,您只需将其复制/粘贴到 Visual Studio(或任何首选软件)中的新控制台应用程序项目上,然后运行它即可查看这是如何运作的。该示例是在著名的 Northwind 数据库上实现的。
OleDbConnection cn = new OleDbConnection();
OleDbCommand cmd = new OleDbCommand();
//Open a connection to the SQL Server Northwind database.
// This is the sample DB I have used in my example.
cn.ConnectionString = "Provider=SQLOLEDB;Data Source=SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;";
cn.Open();
//Retrieve records from the Employees table into a DataReader.
cmd.Connection = cn;
cmd.CommandText = "SELECT * FROM Employees";
//Retrieve column schema into a constraints.
var schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Check_Constraints,null);
//For each field in the table...
foreach (DataRow myField in schemaTable.Rows)
{
//For each property of the field...
foreach (DataColumn myProperty in schemaTable.Columns)
{
//Display the field name and value.
Console.WriteLine(myProperty.ColumnName + " = " + myField[myProperty].ToString());
}
Console.WriteLine();
//Pause.
}
Console.WriteLine("Done");
Console.ReadLine();
//Always close the DataReader and connection.
cn.Close();
如果您查看输出,您可以看到应用于 Discount
表的 Discount
字段的约束。
CONSTRAINT_CATALOG = Northwind
CONSTRAINT_SCHEMA = dbo
CONSTRAINT_NAME = CK_Discount
CHECK_CLAUSE = ([Discount]>=(0) AND [Discount]<=(1))
DESCRIPTION =
更新
一般来说,我会建议您熟悉 How To Retrieve Column Schema by Using the DataReader GetSchemaTable Method and Visual C# .NET
下面的示例是上面链接中代码的逐行副本,除了我已向此代码添加了 string
的 List
并捕获了表的字段名称(在本上下文中,在 C# DataColumn
中称为 ColumnName
),并且我已使用 //++ 添加++
标记了我添加的行。
OleDbConnection cn = new OleDbConnection(); OleDbCommand cmd = new OleDbCommand(); 数据表模式表; OleDbDataReader myReader;
//Open a connection to the SQL Server Northwind database.
cn.ConnectionString = "Provider=SQLOLEDB;Data Source=EINSTEINIUM\\SQL2014EXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;Encrypt=False;TrustServerCertificate=False";
cn.Open();
//Retrieve records from the Employees table into a DataReader.
cmd.Connection = cn;
cmd.CommandText = "SELECT * FROM Employees";
myReader = cmd.ExecuteReader(CommandBehavior.KeyInfo);
//Retrieve column schema into a DataTable.
schemaTable = myReader.GetSchemaTable();
// ++ Added ++
var listOfTableFields = new List<string>();
//For each field in the table...
foreach (DataRow myField in schemaTable.Rows)
{
//For each property of the field...
foreach (DataColumn myProperty in schemaTable.Columns)
{
//Display the field name and value.
Console.WriteLine(myProperty.ColumnName + " = " + myField[myProperty].ToString());
// ++ Added ++
if (myProperty.ColumnName == "ColumnName")
{
listOfTableFields.Add(myField[myProperty].ToString());
}
}
Console.WriteLine();
//Pause.
}
//Always close the DataReader and connection.
myReader.Close();
cn.Close();
// ++ Added ++
Console.WriteLine("List of fields in Employees table.");
// List of Fields in the Employees table.
foreach (var fieldName in listOfTableFields)
{
Console.WriteLine(fieldName);
}
Console.ReadLine();
将此代码粘贴到控制台应用程序中并了解如何使用它。然后将您需要的部分移动到按钮 OnClick
上将非常容易。
关于c# - 如何使用 oleDb 检查域约束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28151754/