日期:2014-05-17  浏览次数:20458 次

请教一下各位大侠我的程序哪里错了 谢谢了
关于自定义表单,我的思路是记录创建表单的项然后通过SQL过程创建数据表可是老是提示类型异常请各位帮忙看下到底哪里错了,谢谢

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- 根据要求创建表单
-- =============================================
ALTER PROCEDURE [dbo].[CreateTables]
 @tablename nvarchar(100), --表名
 @str nvarchar(1000) --字段
AS
declare @sql nvarchar(1100)
set @sql=''
BEGIN
set @sql='create table'+' '+@tablename+'('+@str+')'+';'
print @sql
execute (@sql)
  --create table @tablename (@str)
END




asp.net调用如下

 /// <summary>
  /// connect 为连接字符串 
  /// process 为过程名
  /// Sda为 SQL数据适配器
  /// </summary>
  SqlConnection connect;
  SqlCommand process;
  SqlDataAdapter Sda;
  private string connectionString = ConfigurationManager.ConnectionStrings["oasysConnectionString"].ConnectionString;
  public WorkFlow()
  {
  connect = new SqlConnection(connectionString);
  this.process = connect.CreateCommand();
  Sda = new SqlDataAdapter(this.process);
  }

public void CreateTables(string names, string strs)
  {
  connect.Open();
  process = new SqlCommand("CreateTables", connect);
  process.CommandType = CommandType.StoredProcedure;
  SqlParameter tablename = new SqlParameter("@tablename", SqlDbType.NVarChar, 100);
  tablename.Value = names;
  process.Parameters.Add(tablename);
  SqlParameter str = new SqlParameter("@str", SqlDbType.NVarChar, 1000);
  str.Value = strs;
  process.Parameters.Add(str);
  runprocess();
  }
  private void runprocess()
  {
  try
  {
  process.ExecuteNonQuery();
  }
  catch (Exception ex)
  {
  ex = new Exception("操作失败!");

  throw ex;
  }
  finally
  {
  connect.Close();
  }
  }





页面文件



WorkFlow.WorkFlow WorkFlow = new WorkFlow.WorkFlow();
  static string str = "";
  static string route = "";
  protected void Page_Load(object sender, EventArgs e)
  {
  //UpdatePanel1.Visible = true;
  //UpdatePanel2.Visible = false;
  //UpdatePanel3.Visible = false;
  //if (!Page.IsPostBack)
  //{

  //}
  }
  protected void Button1_Click(object sender, EventArgs e)
  {  

  try
  {
  WorkFlow.CreateWorkFlowTable(TextBox1.Text, "id", "int", "0", "IDENTITY(1,1) NOT NULL PRIMARY KEY", TextBox2.Text);
  pClass.MessageBoxShow("创建表成功", "", Page);
  str = TextBox1.Text + " " + "id" + " " + "int" + " " + "" + "IDENTITY(1,1) NOT NULL PRIMARY KEY"; //str记录创建表单的字段参数
  UpdatePanel1.Visible = false;
  UpdatePanel2.Visib