关于SQL创建表的问题
using System;using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 建表
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string bm = textBox1.Text.Trim();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=localhost;Integrated Security=SSPI;database=学生";
conn.Open();
//错误语句
string sqlins = "CREATE TABLE '"+bm+"'(Sno char(10),Sname char(20),Sxi char(20),Schengji char(3))";
//这样是正确的--name是一个表名
//string sqlins = "CREATE TABLE name(Sno char(10),Sname char(20),Sxi char(20),Schengji char(3))";
SqlCommand comm = new SqlCommand();
= sqlins;
comm.Connection = conn;
comm.ExecuteNonQuery();
MessageBox.Show("创建成功!", "提示");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
}
}
}
如果把bm换成一个字段可以正确的创建以个表,但是利用字符创建会弹出以个错误,那位大虾可以指点一下小弟
先谢谢了
[[it] 本帖最后由 a380151346 于 2008-6-12 01:47 编辑 [/it]]