我有错误连接数据库我无法处理代码con.open错误和IP [英] I have a error to connect the database I unable to process the code con.open error and ip

查看:96
本文介绍了我有错误连接数据库我无法处理代码con.open错误和IP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

namespace Client
{
    class Class2
    {

        string constring = Convert.ToString(ConfigurationSettings.AppSettings["ConnectionString"]);
        SqlCommand cmd1, cmd2, c, c1, cmd3;
        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 autoid()
        {
            SqlConnection con4 = new SqlConnection(constring);
            con4.Open();
            SqlCommand c5;
            c5 = new SqlCommand("select max(req_id) from requestfile", 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 fi, string finam, byte[] fibytes, string exe, string path1)
        {

            SqlConnection con2 = new SqlConnection(constring);
            con2.Open();
            int n = finam.Length;
            cmd2 = new SqlCommand("insert into uploadfiles values('" + fi + "','" + finam + "',@files,'" + exe + "','" + path1 + "')", con2);
            cmd2.Parameters.AddWithValue("@files", fibytes);
            cmd2.ExecuteNonQuery();
            con2.Close();


        }

        public void requestfile(string req_id, string Filename, string Username, string ipaddress, string ReqDate_Time)
        {

            SqlConnection con2 = new SqlConnection(constring);
            con2.Open();
           
            cmd2 = new SqlCommand("insert into requestfile values('" + req_id + "','" + Filename + "','" + Username + "','" +ipaddress+"','"+ ReqDate_Time + "')", con2);
          
            cmd2.ExecuteNonQuery();
            con2.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();
        }

    }
}





我尝试过:



i试过但我无法确定如何纠正错误



What I have tried:

i tried that but i unable to identify the how to correct the errors

推荐答案

如果您只是使用异常,您可以找出代码中出现的问题并解决问题。



If you would just use exceptions, you could find out what's going wrong in your code, and addressing the issue.

try
{
    using (SqlConnection con = new SqlConnection(constring))
    {
        con.Open();
        using (SqlCommand c1 = new SqlCommand("select max(ID) from Registration", con))
        {
            int id = Convert.ToString(c1.ExecuteScalar());
            if (id == "")
            {
                eid = 1;
            }
            else
            {
                eid = Convert.ToInt16(id);
                eid = eid + 1;
            }
        }
    }
}
catch (Exception ex)
{ 
   
} // put a breakpoint on this bracket, and when your code fails, inspect "ex"





Visual Studio和.Net为您提供调试代码所需的所有工具。学习使用这些工具。



我要问 - 为什么你自己生成这个ID?数据库可以为您完成,如果在提交之间多次访问数据库,则不会冒着ID冲突的风险。



Visual Studio and .Net give you all the tools you need to debug your code. Learn to use those tools.

And I gotta ask - why are you generating the ID yourself? The database can do it for you, and you don't run the risk of ID collisions in the event of multiple accesses of the database between commits.


这篇关于我有错误连接数据库我无法处理代码con.open错误和IP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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