首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

请问一下各位大侠小弟我的程序错哪了 多谢了

2012-05-03 
请教一下各位大侠我的程序哪里错了 谢谢了关于自定义表单,我的思路是记录创建表单的项然后通过SQL过程创建

请教一下各位大侠我的程序哪里错了 谢谢了
关于自定义表单,我的思路是记录创建表单的项然后通过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.Visible = true;
  UpdatePanel3.Visible = false;

  tablenames.Text = TextBox1.Text;



  }
  catch
  {
  pClass.MessageBoxShow("创建表失败", "", Page);
  }
  }
  protected void Button2_Click(object sender, EventArgs e)
  {
  try
  {
  WorkFlow.CreateWorkFlowTable(tablenames.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text,TextBox7.Text);
  pClass.MessageBoxShow("创建字段成功", "", Page);
  str += "," + tablenames.Text + " " + TextBox3.Text + " " + TextBox4.Text + "(" + TextBox5.Text+")"+ " " + TextBox6.Text + " " + TextBox7.Text;

  GridView1.DataBind();
  TextBox7.Text = "";
  TextBox6.Text = "";
  TextBox5.Text = "";
  TextBox4.Text = "";
  TextBox3.Text = "";
  //待定
  }
  catch
  {
  pClass.MessageBoxShow("创建字段失败", "", Page);
  }
  }
  protected void Button3_Click(object sender, EventArgs e)
  {
  UpdatePanel1.Visible = false;
  UpdatePanel2.Visible = false;
  UpdatePanel3.Visible = true;
  }
  protected void Button4_Click(object sender, EventArgs e)
  {
  GridView3.DataBind();
  }
  protected void LinkButton1_Click(object sender, EventArgs e)
  {
  Label Label1 = (Label)GridView3.FindControl("Label1");
  TextBox TextBox8 = (TextBox)UpdatePanel3.FindControl("TextBox8");
  TextBox8.Text = Label1.Text;
  }
  protected void Button5_Click(object sender, EventArgs e)
  {
  route += TextBox8.Text + "-";
  str += "," + tablenames.Text + " " + TextBox8.Text + "sh" + " " + "nvarchar(200)" + " ";
  WorkFlow.CreateWorkFlowTable(tablenames.Text, TextBox8.Text + "sh", "nvarchar(200)", "200", "", "");
  TextBox8.Text = "";
  TextBox9.Text = "";
  //pClass.MessageBoxShow("节点输入成功", "");
  routelabel.Text = route;
   
  UpdatePanel3.DataBind();
  }
  protected void Button6_Click(object sender, EventArgs e)
  {
  //route += "'";
  str += "," + "route" + " " + "nvarchar(500)" + " " + "default " + route;//记录流程节点
  str += "," + "currents" + " " + "nvarchar(500)";//记录到哪里了
  //str+=","+"
  ///daixu!!!!!!!!!!!!!!!!!!!!!!!!
  }
  protected void Button7_Click(object sender, EventArgs e)
  {
  try
  {
  WorkFlow.CreateTables(tablenames.Text, str);
  pClass.MessageBoxShow("创建成功", "", Page);
  }
  catch
  {
   
   
  }
  }

敢问我错在哪里了 谢谢


[解决办法]
列名怎么都有空格啊,tb8 id,tb8 name...
这是什么东西
------解决方案--------------------


我有点明白了,你的表变量的类型错误,应该是数据库的表类型,而不是varchar或者nvarchar,
解决方法就是把表变量(@tablename)的类型转换好就行了。
[解决办法]
create table tb8 
(
id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
[name] int,
[2sh] nvarchar(200),
[3sh] nvarchar(200),
[4sh] nvarchar(200),
[route] nvarchar(500) default '2-3-4-',
currents nvarchar(500)
)

我执行成功了,你去试试

热点排行