热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SQL多重语句,插入无效-SQLmultiplestatements,INSERTnotworking

Iamdevelopingawebappandhaveencounteredaproblem.Ineedtoinsertusernameandipaddressi

I am developing a web app and have encountered a problem. I need to insert username and ip address into a SQL database table "log" when someone tries (successfully or unsuccessfully) to login. ID, time and date are inserted automatically... For some reason I am unable to get it working in my login form. INSERT statement works ok if I initiate it from another form, but I can't get it working together with the SELECT statement I use for checking login credentials.

我正在开发一个web应用程序,遇到了一个问题。当有人尝试登录(成功或失败)时,我需要将用户名和ip地址插入到SQL数据库表“log”中。自动插入ID、时间和日期……由于某种原因,我无法让它在我的登录表单中工作。如果我从另一个表单发起它,插入语句可以工作,但是我无法让它与用于检查登录凭据的SELECT语句一起工作。

I have tried different solutions, but none of them inserts data into the table... It does not throw an error, it just doesn't insert a new row into "log" table.

我尝试过不同的解决方案,但没有一种方案会将数据插入到表中……它不会抛出错误,只是不会向“log”表插入新行。

Any help is appreciated. :)

任何帮助都是感激。:)

protected void btnLog_Click(object sender, EventArgs e)
{
    using (SqlConnection cOnn= new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString()))
    {
        string username = null;
        string password = null;
        string ipAddress = null;

        SymCryptography cryptic = new SymCryptography();
        SqlCommand cmdSelect = new SqlCommand();
        SqlCommand cmdLog = new SqlCommand();
        SqlDataReader myReader = null;

        cmdSelect.COnnection= conn;
        cmdLog.COnnection= conn;

        cmdSelect.CommandText = "SELECT * FROM uporabniki WHERE up_ime=@up_ime AND geslo=@geslo";
        cmdSelect.CommandType = CommandType.Text;
        cmdLog.CommandText = "INSERT INTO log (up_ime, ip) VALUES (@up_ime, @ip)";
        cmdLog.CommandType = CommandType.Text;

        cmdSelect.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
        cmdSelect.Parameters.Add("@geslo", SqlDbType.NVarChar, 20).Value = cryptic.Encrypt(tbPwd.Text);
        cmdLog.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
        cmdLog.Parameters.Add("@ip", SqlDbType.NVarChar, 20).Value = ipAddress;

        conn.Open();
        try
        {
            //cmdLog.ExecuteNonQuery();  I tried it here, but it doesn't work
            myReader = cmdSelect.ExecuteReader();
            if (myReader.Read())
            {
                username = myReader["up_ime"].ToString();
                password = myReader["geslo"].ToString();
                Session["rights"] = myReader["pravice"];
                Session["login"] = "OK";
                pravice = true;
            }
            myReader.Close();
            //cmdLog.ExecuteNonQuery();  I tried it here, but it doesn't work
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        conn.Close();
    }
    //I tried to open connection again, but stil INSERT does not work
    /* using (SqlConnection cOnn= new SqlConnection(ConfigurationManager.ConnectionStrings["dbConn"].ToString()))
    {
        string ipAddress = null;
        SqlCommand cmdLog = new SqlCommand();
        cmdLog.COnnection= conn;
        cmdLog.CommandText = "INSERT INTO log (up_ime, ip) VALUES (@up_ime, @ip)";
        cmdLog.CommandType = CommandType.Text;
        cmdLog.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
        cmdLog.Parameters.Add("@ip", SqlDbType.NVarChar, 20).Value = ipAddress;
        conn.Open();
        try
        {
            cmdLog.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
        conn.Close();
    }

    if (pravice == true)
    {
        Response.Redirect("Default.aspx");
    }
    else
    {
        Response.Redirect("Login.aspx");
    }*/ 
}

3 个解决方案

#1


3  

Your not executing your cmdLog.ExecuteNonQuery(); statement.

不执行cmdLog.ExecuteNonQuery();声明。

Also, try opening a query window in your sql database and run the following against it.

另外,尝试在sql数据库中打开一个查询窗口,并针对它运行以下操作。

INSERT INTO log (up_ime, ip) VALUES (, )

If the error lies in sql server you should be returned an error message stating if the problem lies withing SQL Server.

如果错误发生在sql server中,则应该返回一条错误消息,说明问题是否存在于sql server中。

Also try changing:

也试着改变:

    cmdLog.Parameters.Add("@up_ime", SqlDbType.NVarChar, 20).Value = tbUsr.Text;
    cmdLog.Parameters.Add("@ip", SqlDbType.NVarChar, 20).Value = ipAddress;

To:

:

  cmdLog.Parameters.Add("@up_ime", tbUsr.Text);
  cmdLog.Parameters.Add("@ip",  ipAddress);

#2


1  

Check whether your connection string value is ok. Also put a break point on the:

检查连接字符串值是否ok。也在:

   conn.Open();

See whether you get an error? If not then that means your connectionstring is ok.

看看是否有错误?如果不是,那么表示connectionstring是ok的。

Uncomment this line after Try{}

尝试{}后取消这行注释

 //cmdLog.ExecuteNonQuery();  I tried it here, but it doesn't work

and put a break point on it .. see whether you get an error?

在上面放一个断点。看看是否有错误?

Also from your code I can see that you are inserting a null ipaddress value into the table. See whether the column in your database is allowed to accept nulls??

从您的代码中,我还可以看到您正在向表中插入一个null ipaddress值。查看数据库中的列是否允许接受nulls?

#3


0  

The using statement will clean up the connection in the garbage collection process which happens at an indeterminable time. This is why 1 of the orders of execution you've tried doesn't work (the connection may not be usable). Suggest you do either:

using语句将清理垃圾收集过程中的连接,这在不可确定的时间内发生。这就是为什么您尝试的执行顺序中有一个不能工作(连接可能不能使用)。建议你:

  1. Both operations within 1 using (connection), make sure you close the connection in a finally() block and re-init your command against the newly opeend connection.

    使用(connection)在1内的两个操作都要在finally()块中关闭连接,并针对新opeend连接重新初始化命令。

  2. Have 2 different connections conn1 and conn2 and perform the separate operations in each.

    拥有两个不同的连接conn1和conn2,并在每个连接中执行不同的操作。

EDIT: I have re-read and you say no errors occur? - try checking the result of ExecuteNonQuery (it returns an integer to show the number of rows that have been affected - if this is zero there is a problem with your insert statement).

编辑:我重新读过,你说没有出现错误?-尝试检查ExecuteNonQuery的结果(它返回一个整数以显示受影响的行数——如果这个为零,那么您的insert语句有问题)。


推荐阅读
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • CSS3选择器的使用方法详解,提高Web开发效率和精准度
    本文详细介绍了CSS3新增的选择器方法,包括属性选择器的使用。通过CSS3选择器,可以提高Web开发的效率和精准度,使得查找元素更加方便和快捷。同时,本文还对属性选择器的各种用法进行了详细解释,并给出了相应的代码示例。通过学习本文,读者可以更好地掌握CSS3选择器的使用方法,提升自己的Web开发能力。 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 本文介绍了RPC框架Thrift的安装环境变量配置与第一个实例,讲解了RPC的概念以及如何解决跨语言、c++客户端、web服务端、远程调用等需求。Thrift开发方便上手快,性能和稳定性也不错,适合初学者学习和使用。 ... [详细]
  • 计算机存储系统的层次结构及其优势
    本文介绍了计算机存储系统的层次结构,包括高速缓存、主存储器和辅助存储器三个层次。通过分层存储数据可以提高程序的执行效率。计算机存储系统的层次结构将各种不同存储容量、存取速度和价格的存储器有机组合成整体,形成可寻址存储空间比主存储器空间大得多的存储整体。由于辅助存储器容量大、价格低,使得整体存储系统的平均价格降低。同时,高速缓存的存取速度可以和CPU的工作速度相匹配,进一步提高程序执行效率。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了在使用Python中的aiohttp模块模拟服务器时出现的连接失败问题,并提供了相应的解决方法。文章中详细说明了出错的代码以及相关的软件版本和环境信息,同时也提到了相关的警告信息和函数的替代方案。通过阅读本文,读者可以了解到如何解决Python连接服务器失败的问题,并对aiohttp模块有更深入的了解。 ... [详细]
  • ASP.NET2.0数据教程之十四:使用FormView的模板
    本文介绍了在ASP.NET 2.0中使用FormView控件来实现自定义的显示外观,与GridView和DetailsView不同,FormView使用模板来呈现,可以实现不规则的外观呈现。同时还介绍了TemplateField的用法和FormView与DetailsView的区别。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • IjustinheritedsomewebpageswhichusesMooTools.IneverusedMooTools.NowIneedtoaddsomef ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 添加环境光使正方体显示更真实
    本文介绍了如何给正方体添加环境光以使其显示更真实。通过在代码中加入环境光的计算,可以让物体的背光部分不再完全黑色,从而增加物体的真实感。代码中使用了顶点属性、光照颜色、光照方向、环境光等参数来计算物体的漫反射,并将计算结果与顶点颜色相乘得到最终的颜色。通过调整环境光的参数,可以达到不同的光照效果。 ... [详细]
author-avatar
pfshi
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有