为什么这个SQL INSERT在没有提升和异常的情况下失败?

时间:2015-03-06 09:39:41

标签: c# sql-server azure sql-insert

我在Azure SQL上的表中INSERT失败了 - 在使用ExecuteNonQuery命令影响的行上返回-1。

表格如下:

URLS Table

,代码是这样的:

private void SaveUrls(ArrayList alUrls)
{
    int rows = 0;
    this.Cursor = Cursors.WaitCursor;

    foreach (string url in alUrls)
    {
        try
        {
            // INSERT INTO URLS (UrlHash,URLPath,DiscoveryDate,IsInProcessingQueue) VALUES('83B2DE6A8247D2F0C1F674DA0DC4E173230C541542D0A4852A8F83AC8A874D43E2A9335B3A505A50DBC735592D792E9A2564FF556EB3286C7F974A0EDE995F46','http://google.com/blog-entry-4835.html', getutcdate(), False)
            // INSERT INTO URLS (UrlHash,URLPath,DiscoveryDate,DiscoveredById,IsInProcessingQueue) VALUES('83B2DE6A8247D2F0C1F674DA0DC4E173230C541542D0A4852A8F83AC8A874D43E2A9335B3A505A50DBC735592D792E9A2564FF556EB3286C7F974A0EDE995F46','http://google.com/blog-entry-4835.html', getutcdate(), 1006, False)
            // INSERT INTO [dbo].[URLS] ([UrlId], [UrlHash], [URLPath], [DiscoveryDate], [DiscoveredById], [ProcessedDate], [ProcessedById], [IsInProcessingQueue], [JoinedProcessingQueueDate]) VALUES (1, N'83B2DE6A8247D2F0C1F674DA0DC4E173230C541542D0A4852A8F83AC8A874D43E2A9335B3A505A50DBC735592D792E9A2564FF556EB3286C7F974A0EDE995F46', N'http://google.com/blog-entry-4835.html', N'2015-03-05 22:44:18', 1006, NULL, NULL, 0, NULL)
            // INSERT INTO URLS ([UrlId], [UrlHash], [URLPath], [DiscoveryDate], [DiscoveredById], [ProcessedDate], [ProcessedById], [IsInProcessingQueue], [JoinedProcessingQueueDate]) VALUES(N'83B2DE6A8247D2F0C1F674DA0DC4E173230C541542D0A4852A8F83AC8A874D43E2A9335B3A505A50DBC735592D792E9A2564FF556EB3286C7F974A0EDE995F46',N'http://google.com/blog-entry-4835.html',N'getutcdate()', 1006, ,,False,)
            // INSERT INTO URLS ([UrlId], [UrlHash], [URLPath], [DiscoveryDate], [DiscoveredById], [ProcessedDate], [ProcessedById], [IsInProcessingQueue], [JoinedProcessingQueueDate]) VALUES(N'83B2DE6A8247D2F0C1F674DA0DC4E173230C541542D0A4852A8F83AC8A874D43E2A9335B3A505A50DBC735592D792E9A2564FF556EB3286C7F974A0EDE995F46',N'http://google.com/blog-entry-4835.html',N'getutcdate()', 1006, '','',False,'')

            SqlCommand command = new SqlCommand("INSERT INTO URLS ([UrlId], [UrlHash], [URLPath], [DiscoveryDate], [DiscoveredById], [ProcessedDate], [ProcessedById], [IsInProcessingQueue], [JoinedProcessingQueueDate]) VALUES(N'" + HashPassword(url) + "',N'" + url + "',N'getutcdate()', 1006, '" + null+ "','" + null + "'," + false + ",'" + null + "')");
            command.CommandType = CommandType.Text;
            command.Connection = con;

            rows += command.ExecuteNonQuery();
        }
        catch (SqlException sqle)
        {
            Log("SQL ERROR: SaveUrls " + sqle.Message);
        }

        Application.DoEvents();
    }

    this.Cursor = Cursors.Default;

    Log("Seeded URLS Table with " + rows.ToString("N0") + " rows");

    alUrls.Clear();           
}

更新

以下是表格的相关性:

Diagram

更新2:

  1. 没有生成异常
  2. 即使删除了UrlId,插入也会失败。

2 个答案:

答案 0 :(得分:3)

您的INSERT语句指定UrlId字段,但您提供的第一个值是哈希值。

INSERT INTO URLS ([UrlId], [UrlHash], [URLPath]...

应该是

INSERT INTO URLS ([UrlHash], [URLPath]...

另外,正如其他人在评论中所说,使用参数而不是构建SQL字符串!

答案 1 :(得分:0)

答案结果是,varchar(128)的URLHash列太小,无法容纳128个字符的字符串!

我不知道它是如何工作的但是在删除约束并将varchar(128)更改为nvarchar(256)之后(不知道为什么它仍然是varchar)我恢复了约束并且嘿presto它工作。