无法向表中插入记录

发布于 2024-07-07 04:27:36 字数 905 浏览 8 评论 0原文

我编写了一个程序,其中包括从数据库写入和读取。 当我运行应用程序并尝试执行写入时,我调用以下方法:

public static void AddMessage(string callID, string content)
    {
        string select =
            "INSERT INTO Sporocilo (oznaka_klica, smer, vsebina, prebrano, cas_zapisa) VALUES (@callId, 0, @content, 0, @insertTime)";
        SqlCommand cmd = new SqlCommand(select, conn);
        cmd.Parameters.AddWithValue("callId", callID.ToString());
        cmd.Parameters.AddWithValue("content", content);
        cmd.Parameters.AddWithValue("insertTime", "10.10.2008");
        try
        {
            conn.Open();
            cmd.ExecuteScalar();
        }
        catch(Exception ex)
        {
            string sDummy = ex.ToString();
        }
        finally
        {
            conn.Close();
        }
    }

方法调用后,我从表中读取所有记录并将它们显示在表单中。 可以看到刷新之前插入的记录,但是当我退出应用程序并查看表格时,我看不到该记录。

有谁知道什么会导致这种行为?

I wrote a program which includes writing and reading from database. When I run the app and try to perform writing I call the following method:

public static void AddMessage(string callID, string content)
    {
        string select =
            "INSERT INTO Sporocilo (oznaka_klica, smer, vsebina, prebrano, cas_zapisa) VALUES (@callId, 0, @content, 0, @insertTime)";
        SqlCommand cmd = new SqlCommand(select, conn);
        cmd.Parameters.AddWithValue("callId", callID.ToString());
        cmd.Parameters.AddWithValue("content", content);
        cmd.Parameters.AddWithValue("insertTime", "10.10.2008");
        try
        {
            conn.Open();
            cmd.ExecuteScalar();
        }
        catch(Exception ex)
        {
            string sDummy = ex.ToString();
        }
        finally
        {
            conn.Close();
        }
    }

After the method call I read all the records from the table and display them in the form. The record inserted before refresh could be seen but then when I exit the app and look at the table I don't see the record.

Does anyone know what could cause such behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵强ㄟ 2024-07-14 04:27:36

您在此之后执行提交吗? 它可能正在运行您的语句,但随后不提交更改并执行隐式回滚。

我认为异常处理看起来很狡猾。 除非你能以某种方式真正处理它,否则抓住它是没有意义的。 框架的顶层是捕获和报告意外异常的地方。

Are you performing a commit after this? It might be running your statement but then not committing the changes and doing an implicit rollback.

I think the exception handling looks dodgy. There is no point catching something unless you can actually handle it in some way. The top level of your framework is the place for catching and reporting unexpected exceptions.

花开浅夏 2024-07-14 04:27:36

您是否尝试过将 ExecuteScalar 方法的返回值设置为 int,然后根据表检查值?

ExecuteScalar -

执行查询,并返回查询返回的结果集中第一行的第一列。 额外的列或行将被忽略

public static int AddMessage(string callID, string content)
    {
        Int32 newProdID = 0
        string select =
            "INSERT INTO Sporocilo (oznaka_klica, smer, vsebina, prebrano, cas_zapisa) VALUES (@callId, 0, @content, 0, @insertTime); SELECT CAST(scope_identity() AS int);";
        SqlCommand cmd = new SqlCommand(select, conn);
        cmd.Parameters.AddWithValue("callId", callID.ToString());
        cmd.Parameters.AddWithValue("content", content);
        cmd.Parameters.AddWithValue("insertTime", "10.10.2008");
        try
        {
            conn.Open();
            newProdID = (Int32)cmd.ExecuteScalar();
        }
        catch(Exception ex)
        {
            string sDummy = ex.ToString();
        }
        finally
        {
            conn.Close();
        }
        return (int)newProdID
    }

Have you tried setting the return value of the ExecuteScalar method to an int and then checking value against the table?

ExecuteScalar -

Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored

public static int AddMessage(string callID, string content)
    {
        Int32 newProdID = 0
        string select =
            "INSERT INTO Sporocilo (oznaka_klica, smer, vsebina, prebrano, cas_zapisa) VALUES (@callId, 0, @content, 0, @insertTime); SELECT CAST(scope_identity() AS int);";
        SqlCommand cmd = new SqlCommand(select, conn);
        cmd.Parameters.AddWithValue("callId", callID.ToString());
        cmd.Parameters.AddWithValue("content", content);
        cmd.Parameters.AddWithValue("insertTime", "10.10.2008");
        try
        {
            conn.Open();
            newProdID = (Int32)cmd.ExecuteScalar();
        }
        catch(Exception ex)
        {
            string sDummy = ex.ToString();
        }
        finally
        {
            conn.Close();
        }
        return (int)newProdID
    }
我不吻晚风 2024-07-14 04:27:36

我发现了问题。 我修改了自动生成的连接字符串

connectionString="数据
源=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\URSZRDB.mdf;集成
安全性=True;用户实例=True"

connectionString="数据
源=.\SQLEXPRESS;AttachDbFilename=C:\Users\Niko\Documents\Visual
Studio 2008\Projects\URSZRWAPChat\URSZRWAPChat\URSZRDB.mdf;集成
安全性=True;用户实例=True"

现在它可以工作了。

这不是我第一次编写这种程序,到目前为止一切都进展顺利......

I found the problem. I modified the automatically generated connection string

connectionString="Data
Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\URSZRDB.mdf;Integrated
Security=True;User Instance=True"

with

connectionString="Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Niko\Documents\Visual
Studio 2008\Projects\URSZRWAPChat\URSZRWAPChat\URSZRDB.mdf;Integrated
Security=True;User Instance=True"

and now it works.

It's not the first time I write this kind of program and so far everything has gone well this way...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文