如何在按钮中单击创建表格 [英] How Do I Create A Table In Button Click In C#

查看:125
本文介绍了如何在按钮中单击创建表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi Friends

如何在C#中创建表.

Hi Friends

How do i create a table in C#.

推荐答案

请参阅以下内容

http://msdn.microsoft.com/zh-cn/library/ms753342(v=vs.110).aspx

将创建一个FlowDocument来托管表,并创建一个新表并将其添加到FlowDocument的内容中.

//创建父级FlowDocument ...
flowDoc =新的FlowDocument();

//创建表...
table1 = new Table();
//...并将其添加到FlowDocument Blocks集合中.
flowDoc.Blocks.Add(table1);


//为表设置一些全局格式设置属性.
table1.CellSpacing = 10;
table1.Background = Brushes.White;
Refer to the following

http://msdn.microsoft.com/en-us/library/ms753342(v=vs.110).aspx

A FlowDocument is created to host the Table, and a new Table is created and added to the contents of the FlowDocument.

// Create the parent FlowDocument...
flowDoc = new FlowDocument();

// Create the Table...
table1 = new Table();
// ...and add it to the FlowDocument Blocks collection.
flowDoc.Blocks.Add(table1);


// Set some global formatting properties for the table.
table1.CellSpacing = 10;
table1.Background = Brushes.White;


如果您需要在访问数据库中创建表,则可以使用OleDbConnection和OleDbCommand类来进行操作,例如:
if you need to create table in access database you can use OleDbConnection and OleDbCommand classes to do it, for example:
OleDbConnection conn = new OleDbConnection(connectionString);
OleDbCommand cmmd = new OleDbCommand("CREATE TABLE  TableName ( [ID] Counter Primary Key, [FirstName] Text, [LastName] Text)", conn);
con.Open();
cmmd.ExecuteNonQuery();


请参考 https://www.connectionstrings.com/access/ [


refer https://www.connectionstrings.com/access/[^] for how you can give connection string for access database.


谢谢老板.它的工作原理.

private void button1_Click(对象发送者,EventArgs e)
{

OleDbConnection myConnection =新的OleDbConnection(@"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\ Users \ Administrator \ Documents \ Database1.accdb");

myConnection.Open();

OleDbCommand myCommand =新的OleDbCommand();
myCommand.Connection = myConnection;
字符串qry =创建表[Database3]([Ename]文本,[ESurname]文本)";


myCommand.CommandText = qry.Replace("Database3",textBox1.Text);
myCommand.ExecuteNonQuery();
MessageBox.Show(表创建成功");
myConnection.Close();
}
Thank You Boss. Its Working Good.

private void button1_Click(object sender, EventArgs e)
{

OleDbConnection myConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Administrator\Documents\Database1.accdb");

myConnection.Open();

OleDbCommand myCommand = new OleDbCommand();
myCommand.Connection = myConnection;
string qry = "CREATE TABLE [Database3]([Ename] text, [ESurname] text)";


myCommand.CommandText = qry.Replace("Database3", textBox1.Text);
myCommand.ExecuteNonQuery();
MessageBox.Show("Table Created Successfully");
myConnection.Close();
}


这篇关于如何在按钮中单击创建表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆