编辑

2012-03-04 38 views
0

我有一个Ajax编辑器在我的网页:编辑

<cc1:Editor ID="Editor1" runat="server" width="600px"/> 

我想是为了节省从编辑到我database.I试过,但它不会工作内容:

SqlCommand cmd = new SqlCommand(
    "INSERT INTO titlu (descriere) Values(@descriere)",con); 

cmd.Parameters.AddWithValue("@descriere", Editor1.Content); 

我使用C#,这是一个ASP.Net Web应用程序。为什么我不能挽救我的数据?

+0

启动调试器 - 进入它 - 然后如果你不能看到什么是错误的后代码的地方行为不同,你期望的。 – dice 2012-03-04 17:25:24

+0

这是它的行为不同的部分..我可以从编辑器中获取值并将其插入到我的数据库中 – Rares 2012-03-04 18:59:16

回答

0

您的代码不显示您执行SQL命令的位置。如果执行命令 错误代码或异常,你会得到什么?

看看这个例子:

// Given command text and connection string, asynchronously execute 
    // the specified command against the connection. For this example, 
    // the code displays an indicator as it is working, verifying the 
    // asynchronous behavior. 
    using (SqlConnection connection = 
       new SqlConnection(connectionString)) 
    { 
     try 
     { 
      int count = 0; 
      SqlCommand command = new SqlCommand(commandText, connection); 
      connection.Open(); 

      IAsyncResult result = command.BeginExecuteNonQuery(); 
      while (!result.IsCompleted) 
      { 
       Console.WriteLine("Waiting ({0})", count++); 
       // Wait for 1/10 second, so the counter 
       // does not consume all available resources 
       // on the main thread. 
       System.Threading.Thread.Sleep(100); 
      } 
      Console.WriteLine("Command complete. Affected {0} rows.", 
       command.EndExecuteNonQuery(result)); 
     } 
     catch (SqlException ex) 
     { 
      Console.WriteLine("Error ({0}): {1}", ex.Number, ex.Message); 
     } 
     catch (InvalidOperationException ex) 
     { 
      Console.WriteLine("Error: {0}", ex.Message); 
     } 
     catch (Exception ex) 
     { 
      // You might want to pass these errors 
      // back out to the caller. 
      Console.WriteLine("Error: {0}", ex.Message); 
     } 
    } 
1

假设你的代码是这样的:

using (SqlConnection con = new ...) 
{ 
    SqlCommand cmd = new SqlCommand(
        "INSERT INTO titlu (descriere) Values(@descriere)",con); 
    cmd.Parameters.AddWithValue("@descriere", Editor1.Content); 
    con.Open(); 
    int affectedRows = cmd.ExecuteNonQuery(); 
} 

后再行cmd.ExecuteNonQuery()要么抛出一个异常,或者返回受影响的行数 - 在您的情况它应该显然是1.

如果没有抛出异常,那么值被输入到数据库 - 确保Editor1.Content a当它在这里被访问时,包含一些东西。还要确保你没有吞咽异常。