不想一次又一次地重复我的代码 [英] do not want to repeat my code again and again

查看:73
本文介绍了不想一次又一次地重复我的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个文件,一个是类filedownload.cs,其中我有三个函数... GetFileLIST()savefile()和GetaFile()。第二个文件是GetFile.aspx,第三个是代码隐藏按钮的形式。使用所有这些文件,我上传一个文件,并在gridview中显示,它正在为我工​​作。但现在我有另一种形式,我想使用代码上传文件。但对于另一种形式,我必须重复所有这些代码,因为查询,因为我现在有不同的表...

请解决此问题,使我的代码简短,不要重复....



filedownload.cs



 public static DataTable GetFileList()
{
DataTable dt = new DataTable();
using(SqlConnection con = new SqlConnection())
{
OpenConnection(con);
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandTimeout = 0;
com.CommandText =SELECT ID,Name,[Content],size FROM Connectivity;
com.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
da.Fill(dt);
con.Close();
}




返回dt;

}
public static void savefile(string name,string content,int size,byte [] data)
{
using(SqlConnection con = new SqlConnection() )
{
OpenConnection(con);
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandTimeout = 0;
string ct =INSERT INTO Connectivity VALUES(@ name,@ content,;
ct = ct +@ size,@ data); //想要不同的查询不同的表
com.CommandText = ct;
com.CommandType = CommandType.Text;
com.Parameters.Add(@ name,SqlDbType.VarChar,50);
com.Parameters.Add(@ content,SqlDbType.VarChar,50);
com.Parameters.Add(@ size,SqlDbType.Int);
com.Parameters.Add(@ data,SqlDbType.VarBinary);

com.Parameters [@ Name]。Value = name;
com.Parameters [@ content]。Value = content;
com.Parameters [@ size]。值=大小;
com.Parameters [@ data]。值=数据;
com.ExecuteNonQuery();
con.Close();


}
}
public static DataTable Getafile(int id)
{
DataTable dt = new DataTable();
using(SqlConnection con = new SqlConnection())
{
OpenConnection(con);
SqlCommand com = new SqlCommand();
com.Connection = con;
com.CommandTimeout = 0;
com.CommandText =SELECT ID,Name,[Content],size,data FROM Connectivity,其中ID = @ID;
com.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter();
com.Parameters.Add(@ ID,SqlDbType.Int);
com.Parameters [@ ID]。Value = id;
da.SelectCommand = com;
da.Fill(dt);
con.Close();


}
return dt;
}

/

解决方案

更改你的函数/程序/ subs / whatever以便它们接受存储过程名称(更好)或SQL字符串(不太好,但足够)作为参数,并将其用作CommandText项...


I建议你阅读:最佳编码惯例 [ ^ ]和程序优化 [ ^ ],然后阅读数据访问层(DAL) [ ^ ]。



资源:

创建数据访问层 [ ^ ]

使用ADO.NET为您的应用程序实现数据访问层 [ ^ ]

ASP.NET 2.0的电源编程技巧 [ ^ ]

http://www.codeguru.com/csharp/article.php/c17299/C-Programming-Tips-and-Tricks.htm [ ^ ]

I have three files one is class filedownload.cs in which i have three functions...GetFileLIST() savefile() and GetaFile().second file is GetFile.aspx and third is form in which code is behind button.using all these files i am uploading a file and showing in gridview and it is working for me.but now i have another form in which i want to use the code for uploading file.but for another form i have to repeat all these code because of query because i have differet table now...
please resolve this problem to make my code short and not to repeat....

filedownload.cs

  public static DataTable GetFileList()
        {
            DataTable dt=new DataTable();
            using (SqlConnection con = new SqlConnection())
            {
                OpenConnection(con);
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandTimeout = 0;
                com.CommandText = "SELECT ID, Name, [Content], size FROM Connectivity";
                com.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = com;
                da.Fill(dt);
                con.Close();
            }
               


            
            return dt;

        }
        public static void savefile(string name, string content, int size, byte[] data)
        {
            using (SqlConnection con = new SqlConnection()) 
            {
                OpenConnection(con);
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandTimeout = 0;
                string ct = "INSERT INTO Connectivity VALUES (@name,@content,";
                ct = ct + "@size,@data)"; // want different query for diffrent table
                com.CommandText = ct;
                com.CommandType = CommandType.Text;
                com.Parameters.Add("@name", SqlDbType.VarChar, 50);
                com.Parameters.Add("@content", SqlDbType.VarChar, 50);
                com.Parameters.Add("@size", SqlDbType.Int);
                com.Parameters.Add("@data", SqlDbType.VarBinary);

                com.Parameters["@Name"].Value = name;
                com.Parameters["@content"].Value = content;
                com.Parameters["@size"].Value = size;
                com.Parameters["@data"].Value = data;
                com.ExecuteNonQuery();
                con.Close();


            }
        }
        public static DataTable Getafile(int id)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection())
            {
                OpenConnection(con);
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandTimeout = 0;
                com.CommandText = "SELECT ID, Name, [Content], size, data FROM Connectivity where ID=@ID";
                com.CommandType = CommandType.Text;
                SqlDataAdapter da = new SqlDataAdapter();
                com.Parameters.Add("@ID", SqlDbType.Int);
                com.Parameters["@ID"].Value = id;
                da.SelectCommand = com;
                da.Fill(dt);
                con.Close();


            }
            return dt;
        }

/

解决方案

Change your functions/procedures/subs/whatever so that they take in either a Stored Procedure name (better) or an SQL string (not quite as good, but will suffice) as a parameter, and use that as the CommandText item...


I would suggest you to read this: Best Coding Practices[^] and Program optimization[^], then read about Data Access Layer (DAL)[^].

Resources:
Creating a Data Access Layer[^]
Implement a Data Access Layer for Your App with ADO.NET[^]
Power Programming Tips for ASP.NET 2.0[^]
http://www.codeguru.com/csharp/article.php/c17299/C-Programming-Tips-and-Tricks.htm[^]


这篇关于不想一次又一次地重复我的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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