我必须找到在数据库(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)
{
}
}
}
答案 0 :(得分:3)
为了检查桌面上应用的约束,您希望使用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
以下示例是上述链接中代码的逐行副本,但我已在此代码中添加了List
string
并捕获了表的字段名称(称为ColumnName
在此上下文中的C#DataColumn
中,我已使用// ++ Added ++
标记了我添加的行。
OleDbConnection cn = new OleDbConnection(); OleDbCommand cmd = new OleDbCommand(); DataTable schemaTable; 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
。