如何使用 oleDb 检查域约束?
How to check for domain Constraints using oleDb?
我必须找到数据库 (MS Access) 中没有域约束的每一列,并为每一列计算当前数据的最小值和最大值,然后从我的程序中添加相应的约束。
例如,列"Foo"的最小值为0,最大值为100,我需要约束"Foo between 0 And 100"。
如何在 C# 中检查列是否具有此约束?
在访问中,我在 "Validation rules" 找到了这个。
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)
{
}
}
}
为了检查对您的 table 施加的约束,您希望使用 OleDbSchemaGuid.Check_Constraints Field。如何使用它实际上与您所说的 OleDbSchemaGuid.Tables
的使用方式略有不同。
为了帮助您解决这个问题,我为您编写了一个小的控制台应用程序,您可以简单地 copy/paste 在 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
table.
的 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
下面的示例是上面 link 中代码的逐行副本,只是我在该代码中添加了 List
of string
并捕获了 table 的字段名称(在 C# 中称为 ColumnName
DataColumn
),并且我已经用 // ++ Added ++
.
标记了我添加的行
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
.
将非常容易
我必须找到数据库 (MS Access) 中没有域约束的每一列,并为每一列计算当前数据的最小值和最大值,然后从我的程序中添加相应的约束。
例如,列"Foo"的最小值为0,最大值为100,我需要约束"Foo between 0 And 100"。
如何在 C# 中检查列是否具有此约束?
在访问中,我在 "Validation rules" 找到了这个。
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)
{
}
}
}
为了检查对您的 table 施加的约束,您希望使用 OleDbSchemaGuid.Check_Constraints Field。如何使用它实际上与您所说的 OleDbSchemaGuid.Tables
的使用方式略有不同。
为了帮助您解决这个问题,我为您编写了一个小的控制台应用程序,您可以简单地 copy/paste 在 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
table.
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
下面的示例是上面 link 中代码的逐行副本,只是我在该代码中添加了 List
of string
并捕获了 table 的字段名称(在 C# 中称为 ColumnName
DataColumn
),并且我已经用 // ++ Added ++
.
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
.