简体   繁体   English

无法将记录插入表中

[英]can't insert a record into table

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? 有谁知道会导致这种行为的原因吗?

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. 框架的顶层是捕获和报告意外异常的地方。

Have you tried setting the return value of the ExecuteScalar method to an int and then checking value against the table? 您是否尝试过将ExecuteScalar方法的返回值设置为int,然后对照表进行检查?

ExecuteScalar - 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
    }

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" 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" 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... 这不是我第一次编写此类程序,到目前为止,一切都进展顺利...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM