C#和SQL项目exe表单和代码 [英] C# and SQL project exe to forms and codes

查看:73
本文介绍了C#和SQL项目exe表单和代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个由visaul studio asp.net和C#.net和sql server 2005构建的网站上下载项目



i download a project in a website its built by visaul studio asp.net and C#.net and sql server 2005

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;

namespace Server
{
    class Class1
    {

        string constring = Convert.ToString(ConfigurationSettings.AppSettings["ConnectionString"]);
        SqlCommand cmd1, cmd2, c, c1, cmd3,cmd7;
        string id, id1, id2, id3, id4;
        int eid, eid1, eid2, eid3, eid4;

        public int idgeneration()
        {
            SqlConnection con = new SqlConnection(constring);
            con.Open();
            SqlCommand c1;
            c1 = new SqlCommand("select max(ID) from Registration", con);
            id = Convert.ToString(c1.ExecuteScalar());
            if (id == "")
            {
                eid = 1;
            }
            else
            {
                eid = Convert.ToInt16(id);
                eid = eid + 1;
            }
            con.Close();
            return eid;
        }

        public int uploadfileid()
        {
            SqlConnection con1 = new SqlConnection(constring);
            con1.Open();
            SqlCommand c1;
            c1 = new SqlCommand("select max(fileid) from uploadfiles", con1);
            id1 = Convert.ToString(c1.ExecuteScalar());
            if (id1 == "")
            {
                eid1 = 1;
            }
            else
            {
                eid1 = Convert.ToInt16(id1);
                eid1 = eid1 + 1;
            }
            con1.Close();
            return eid1;
        }



        public int senditems()
        {
            SqlConnection con1 = new SqlConnection(constring);
            con1.Open();
            SqlCommand c1;
            c1 = new SqlCommand("select max(Req_ID) from senditems", con1);
            id1 = Convert.ToString(c1.ExecuteScalar());
            if (id1 == "")
            {
                eid1 = 1;
            }
            else
            {
                eid1 = Convert.ToInt16(id1);
                eid1 = eid1 + 1;
            }
            con1.Close();
            return eid1;
        }



        public int autoid()
        {
            SqlConnection con4 = new SqlConnection(constring);
            con4.Open();
            SqlCommand c5;
            c5 = new SqlCommand("select max(autid) from usersearch", con4);
            id2 = Convert.ToString(c5.ExecuteScalar());
            if (id2 == "")
            {
                eid2 = 1;
            }
            else
            {
                eid2 = Convert.ToInt16(id2);
                eid2 = eid2 + 1;
            }
            con4.Close();
            return eid2;
        }
        public void uploadfile(string fid, string finame, byte[] File, string time, string path1,string ext)
        {

            SqlConnection con2 = new SqlConnection(constring);
            con2.Open();
            int n = finame.Length;
            cmd2 = new SqlCommand("insert into uploadfiles values('" + fid + "','" + finame + "',@File,'" + time + "','" + path1 + "','"+ext+"')", con2);
            cmd2.Parameters.AddWithValue("@File", File);
            cmd2.ExecuteNonQuery();
            con2.Close();
        }





        public void senditems(string reqid, string sendid, string fname, byte[] File, string uname, string userip, string send)
        {

            SqlConnection con7 = new SqlConnection(constring);
            con7.Open();
            int n = fname.Length;
            cmd7 = new SqlCommand("insert into senditems values('" + reqid + "','" + sendid + "','" + fname + "',@File,'" + uname + "','" + userip + "','"+send+"')", con7);
            cmd7.Parameters.AddWithValue("@File", File);
            cmd7.ExecuteNonQuery();
            con7.Close();
        }



       
        public void usersearch(string auid, string reqid, string usrid, string unam, string filnm, string dat, string sta)
        {
            SqlConnection con3 = new SqlConnection(constring);
            con3.Open();
            cmd3 = new SqlCommand("insert into usersearch values('" + auid + "','" + reqid + "','" + usrid + "','" + unam + "','" + filnm + "','" + dat + "','Key not send')", con3);
            cmd3.ExecuteNonQuery();
            con3.Close();
        }
        public void keymaintain(string aid, string reqid, string usrid, string secretky, string path, string path1)
        {
            SqlConnection con4 = new SqlConnection(constring);
            con4.Open();
            cmd1 = new SqlCommand("insert into keymaintenance values('" + aid + "','" + reqid + "','" + usrid + "','" + secretky + "','" + path + "','')", con4);
            cmd1.ExecuteNonQuery();
            con4.Close();
        }


    }

}





我尝试了什么:





What I have tried:

problem on connecting string so please help me how to connect

推荐答案

cmd3 = new SqlCommand("insert into usersearch values('" + auid + "','" + reqid + "','" + usrid + "','" + unam + "','" + filnm + "','" + dat + "','Key not send')", con3);



不是你的问题的解决方案on,但是你有另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


带有Windows身份验证的 SQL Server Express 的连接字符串示例:

Example connection string for SQL Server Express with Windows authentication:
Connection Timeout=2;Integrated Security=SSPI;Persist Security Info=False;Data Source=MyPCname\SQLEXPRESS;Initial Catalog=MyDatabase

MyPCname 替换为PC的名称或(本地),将 MyDatabase 替换为您的数据库名称。

Replace MyPCname with the name of your PC or (local), replace MyDatabase with the name of your database.


这篇关于C#和SQL项目exe表单和代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆