RT,本人是初学者,现在有个问题,请问下高手,要做个数据库应用,创建好的表单,有若干个文本框输入。在文本框里输入的作为在数据库里新创建表的字段。以后这个表可以继续用来存取和更新数据。要如何做?高手提示下,指点下。不胜感激!
解决方案 »
- 这个子查询错的 我该怎么改请教!
- ASP.NET 2.0中构造个性化网页
- 用.net自带的函数发不出邮件怎么会事,iis的smtp配置了,瑞星也监控到邮件发送了,但是就是收不到。
- 如何把DataTable添加到dataset中?
- datagrid绑定的问题
- 我的DataGrid是用自动生成列,又加了两个按钮列,为什么钮列总是出现在自动生成的列后面
- 怎么用jquery做类似编程软件里面的智能提示,一直找不到资料
- 存入数据库字段中的<br>如何在出来?
- 水晶报表的问题?是BUG吗?
- 过几天我要到学校去聘用asp.net程序员,大家给我出主意!
- 关于网页图片下载问题
- asp.net存取数据库图片问题
string tableName=this.textBox1.Text;
string add_table="create table " + tableName + " (num NVarChar(16) not null primary key)";
SqlCommand comm=new SqlCommand(add_table,conn);
comm.CommandType= CommandType.Text;
comm.Connection.Open();
comm.ExecuteNonQuery();
conn.Close();
comm.Dispose();
就是把文本框输入的内容拼凑成SQL语句。
下面的代码的表结构在DataTable中,楼主把那个循环替换成需要的就可以private void TableCheck()
{
// Create an OleDb database connection using the connection string
// provided when the web form is submitted
OleDbConnection oledbConn = new OleDbConnection(textBoxOleDb.Text);
try
{
// Open the database connection
oledbConn.Open();
// Retrieve database schema information for only the table we are looking for
// In this example the table name is the name of the XML file without the extension
DataTable schemaTable = oledbConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new object[] { null, null, tableName, "TABLE" });
String sqlCmd = "";
// Check to see if the table exists in the database schema
// If the table exists in the schema there will be 1 row in the DataTable
// If the table does not exist in the schema the DataTable row count will be zero
if (schemaTable.Rows.Count < 1)
{
// Make the create table sql command by iterating through the XML file's
// columns. This way the database columns will have the same name as the XML file.
sqlCmd = "create table " + tableName + " (";
for (int i = 0; i < dataTableXml.Columns.Count; i++)
{
// This adds each column as a text/string type with a length of 100
sqlCmd = sqlCmd + dataTableXml.Columns[i].ColumnName.ToString() + " char(100),";
}
// Remove the last ","
sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 1) + ");";
// Create and execute the create table command
OleDbCommand oledbCmd = new OleDbCommand(sqlCmd, oledbConn);
oledbCmd.ExecuteNonQuery();
}
}
catch
{
// If there are errors you will get this message
Message.Text = "The table could not be created or database does not exist.";
}
finally
{
// Close the database connection
oledbConn.Close();
}
}