这是我的代码:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.Common;
public class General_function
{
internal System.Windows.Forms.DataGridView DG_ItemShow;
public static object Get_Single_Value(string SQLQuery)
{
try
{
object SingleValue = null;
DbConnection cn = database_Object.GetConnection(database_Object.Provider);
DbCommand cmd = database_Object.GetCommand(database_Object.Provider);
if (cn.State == ConnectionState.Closed)
{
cn.ConnectionString = My.Settings.Trasns_DataConnectionString;
cn.Open();
}
cmd.Connection = cn;
cmd.CommandText = SQLQuery;
SingleValue = cmd.ExecuteScalar;
return SingleValue;
}
catch (Exception ex)
{
Interaction.MsgBox("error in Get_Single_Value : " + ex.Message);
}
}
public static long get_Max_value(string tablename, string fieldname)
{
try
{
long maxvalue = 0;
DbConnection cn = database_Object.GetConnection(database_Object.Provider);
DbCommand cmd = database_Object.GetCommand(database_Object.Provider);
if (cn.State == ConnectionState.Closed)
{
cn.ConnectionString = My.Settings.Trasns_DataConnectionString;
cn.Open();
}
cmd.Connection = cn;
cmd.CommandText = "select max(" + fieldname + ") From " + tablename;
string res = null;
res = cmd.ExecuteScalar.ToString;
if (string.IsNullOrEmpty(res))
{
maxvalue = 0;
}
else
{
maxvalue = long.Parse(res);
}
return maxvalue;
}
catch (Exception ex)
{
Interaction.MsgBox("Error in get_Max_value" + ex.Message);
}
}
public static int Save_Record(ArrayList Al, string TableName)
{
try
{
int Retval = 0;
Trasns_DataDataSet ds = new Trasns_DataDataSet();
DataTable dt = ds.Tables(TableName);
ArrayList tal = new ArrayList();
foreach (DataColumn cl in dt.Columns)
{
tal.Add(cl.ColumnName);
}
DbConnection cnn = default(DbConnection);
cnn = database_Object.GetConnection(database_Object.Provider);
cnn.ConnectionString = My.Settings.Trasns_DataConnectionString;
cnn.Open();
DbCommand cmd1 = default(DbCommand);
cmd1 = database_Object.GetCommand(database_Object.Provider);
cmd1.Connection = cnn;
string n = null;
for (int i = 0; i <= tal.Count - 1; i++)
{
n = "@" + tal(i).ToString;
DbParameter pa = database_Object.GetParameter(database_Object.Provider);
pa.ParameterName = n;
pa.Value = Al(i);
cmd1.Parameters.Add(pa);
}
n = "";
string v = "";
string sqlstr = "insert into " + TableName + " (";
for (int i = 0; i <= tal.Count - 1; i++)
{
n = n + "," + tal(i).ToString;
v = v + ",@" + tal(i).ToString;
}
n = Strings.Right(n, Strings.Len(n) - 1);
v = Strings.Right(v, Strings.Len(v) - 1);
sqlstr = sqlstr + n + ") values (" + v + ")";
cmd1.CommandText = sqlstr;
Retval = cmd1.ExecuteNonQuery;
return Retval;
}
catch (Exception ex)
{
Interaction.MsgBox("error in Save_Record : " + ex.Message);
}
}
public static int Delete_Record(ArrayList AlName, ArrayList AlValue, string TableName)
{
try
{
int Retval = 0;
DbConnection cnn = default(DbConnection);
cnn = database_Object.GetConnection(database_Object.Provider);
cnn.ConnectionString = My.Settings.Trasns_DataConnectionString;
cnn.Open();
DbCommand cmd1 = default(DbCommand);
cmd1 = database_Object.GetCommand(database_Object.Provider);
cmd1.Connection = cnn;
string m = null;
for (int i = 0; i <= AlName.Count - 1; i++)
{
m = "@" + AlName(i).ToString;
DbParameter pa = database_Object.GetParameter(database_Object.Provider);
pa.ParameterName = m;
pa.Value = AlValue(i);
cmd1.Parameters.Add(pa);
}
string sqlstr = "delete from " + TableName + " where ";
string v = "";
for (int i = 0; i <= AlName.Count - 1; i++)
{
v = v + " And " + AlName(i).ToString + "=@" + AlName(i).ToString;
}
v = Strings.Right(v, Strings.Len(v) - 4);
sqlstr = sqlstr + v;
cmd1.CommandText = sqlstr;
Retval = cmd1.ExecuteNonQuery;
return Retval;
}
catch (Exception ex)
{
Interaction.MsgBox("error in Delete_Record : " + ex.Message);
}
}
public static int Modify_Record(ArrayList AlName, ArrayList AlValue, ArrayList
AlPKName, ArrayList AlPKValue, string TableName)
{
try
{
int Retval = 0;
DbConnection cnn = default(DbConnection);
cnn = database_Object.GetConnection(database_Object.Provider);
cnn.ConnectionString = My.Settings.Trasns_DataConnectionString;
cnn.Open();
DbCommand cmd1 = default(DbCommand);
cmd1 = database_Object.GetCommand(database_Object.Provider);
cmd1.Connection = cnn;
string m = null;
//values parameters
for (int i = 0; i <= AlName.Count - 1; i++)
{
m = "@" + AlName(i).ToString;
DbParameter pa = database_Object.GetParameter(database_Object.Provider);
pa.ParameterName = m;
pa.Value = AlValue(i);
cmd1.Parameters.Add(pa);
}
//primary key column parameters
for (int i = 0; i <= AlPKName.Count - 1; i++)
{
m = "@" + AlPKName(i).ToString;
DbParameter pa = database_Object.GetParameter(database_Object.Provider);
pa.ParameterName = m;
pa.Value = AlPKValue(i);
cmd1.Parameters.Add(pa);
}
string sqlstr = "update " + TableName + " set ";
string v = "";
for (int i = 0; i <= AlName.Count - 1; i++)
{
v = v + "," + AlName(i).ToString + "=@" + AlName(i).ToString;
}
string w = "";
for (int i = 0; i <= AlPKName.Count - 1; i++)
{
w = w + " And " + AlPKName(i).ToString + "=@" + AlPKName(i).ToString;
}
v = Strings.Right(v, Strings.Len(v) - 1);
w = Strings.Right(w, Strings.Len(w) - 4);
sqlstr = sqlstr + v + " Where " + w;
cmd1.CommandText = sqlstr;
Retval = cmd1.ExecuteNonQuery;
return Retval;
}
catch (Exception ex)
{
Interaction.MsgBox("error in Modify_Record : " + ex.Message);
}
}
public static DataTable RecordSearch(string SqlString, string TableName)
{
try
{
DbConnection cnn = default(DbConnection);
cnn = database_Object.GetConnection(database_Object.Provider);
cnn.ConnectionString = My.Settings.Trasns_DataConnectionString;
cnn.Open();
Trasns_DataDataSet ds = new Trasns_DataDataSet();
DataTable dt = default(DataTable);
DbDataAdapter da = database_Object.GetAdapter(database_Object.Provider);
DbCommand cmd = database_Object.GetCommand(database_Object.Provider);
cmd.Connection = cnn;
cmd.CommandText = SqlString;
da.SelectCommand = cmd;
da.Fill(ds, TableName);
dt = ds.Tables(TableName);
cnn.Close();
cmd.Dispose();
da.Dispose();
cnn.Dispose();
return dt;
}
catch (Exception ex)
{
Interaction.MsgBox("error in RecordSearch : " + ex.Message);
}
}
}
如何从其他页面调用此类方法?
答案 0 :(得分:2)
使用静态方法,只需通过引用命名空间来调用它们:
object result = General_function.Get_Single_Value("select * from table");
答案 1 :(得分:1)
如代码所示,您将方法定义为static
,以便调用您可以使用的方法,
var o = General_function.Get_Single_Value("your_parameter");
和
long lobj = General_function.get_Max_value("table_name","field_name");
依旧......