ASP.NET中的ajax编辑器

本文关键字:编辑器 ajax 中的 NET ASP | 更新日期: 2023-09-27 18:26:42

我的页面中有一个Ajax编辑器:

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

我想要的是将编辑器中的内容保存到我的数据库中。我试过了,但不起作用:

SqlCommand cmd = new SqlCommand(
    "INSERT INTO titlu (descriere) Values(@descriere)",con);
cmd.Parameters.AddWithValue("@descriere", Editor1.Content);

我使用的是C#,它是一个ASP.Net web应用程序。。为什么我不能保存我的数据?

ASP.NET中的ajax编辑器

假设您的代码是这样的:

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在此处访问时确实包含某些内容。还要确保你没有吞下例外。

您的代码不会显示在哪里执行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);
        }
    }