麻烦大家帮忙看看有错吗?我用vb.net转的c#的一个简单数据层

zywinner168 2008-11-03 01:05:24

using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System;



public class WDABase : System.IDisposable
{


public string SQL;
public string SQLClStr;
public SqlConnection SQLConnectionStr;
public SqlCommand SQLCommand;
public SqlDataAdapter SQLDataAdapter;
public SqlDataReader SQLDataReader;
public string MyConString = "Data Source=EsI;Initial Catalog=LkDBM;User ID=sa;Password=abcd1234a/";
public string MySevers = "EsI";
public string MyDBMS = "LkDBM";
public string MyLogin = "sa";
public string MyPW = "abcd1234a/";
public string MyTestConStr;

// 检测冗余的调用
private bool disposedValue = false;

// IDisposable
protected virtual void Dispose(bool disposing)
{
if (!this.disposedValue)
{
if (disposing)
{
// TODO: 显式调用时释放非托管资源
if ((SQLConnectionStr != null))
{
SQLConnectionStr.Close();
SQLConnectionStr.Dispose();
SQLConnectionStr = null;
}
if ((SQLDataReader != null))
{
SQLDataReader.Close();
SQLDataReader = null;
}
if ((SQLDataAdapter != null))
{
SQLDataAdapter.Dispose();
SQLDataAdapter = null;
}
if ((SQLCommand != null))
{
SQLCommand.Dispose();
SQLCommand = null;
}
}
// TODO: 释放共享的非托管资源
}
this.disposedValue = true;
}

public WDABase()
{
MyTestConStr = "Data Source=" + MySevers + ";" + "Initial Catalog=" + MyDBMS + ";" + "User ID=" + MyLogin + ";" + "Password=" + MyPW;
SQLConnectionStr = new SqlConnection(MyTestConStr);
}

public void SQLOpenConnection()
{
try
{
SQLConnectionStr.Open();
}

catch (System.Exception OleDBExceptionErr)
{
throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);

}
}

public void SQLCloseConnection()
{
SQLConnectionStr.Close();
}

public void InitializeSQLCommand()
{
if (SQLCommand == null)
{
try
{
SQLCommand = new SqlCommand(SQL, SQLConnectionStr);
if (!SQL.ToUpper().StartsWith("SELECT") & !SQL.ToUpper().StartsWith("INSERT") & !SQL.ToUpper().StartsWith("UPDATE") & !SQL.ToUpper().StartsWith("DELECT"))
{
SQLCommand.CommandType = CommandType.StoredProcedure;

}
}
catch (System.Exception OleDBExceptionErr)
{
throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);
}

}
}


public void SQLAddParameter(string Name, SqlDbType Type, int Size, object Value)
{
try
{
SQLCommand.Parameters.Add(Name, Type, Size).Value = Value;
}
catch (SqlException OleDbExceptionErr)
{
throw new System.Exception(OleDbExceptionErr.Message, OleDbExceptionErr.InnerException);

}
}

public void InitializeSQLDataAdapter()
{
try
{
SQLDataAdapter = new SqlDataAdapter();
SQLDataAdapter.SelectCommand = SQLCommand;
}

catch (SqlException OleDbExceptionErr)
{
throw new System.Exception(OleDbExceptionErr.Message, OleDbExceptionErr.InnerException);

}
}

public void FillSQLDataSet(ref DataSet oDataSet, string TableName)
{
try
{
InitializeSQLCommand();
InitializeSQLDataAdapter();
SQLDataAdapter.Fill(oDataSet, TableName);
}
catch (System.Exception OleDBExceptionErr)
{
throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);

}
SQLCommand.Dispose();
SQLCommand = null;
SQLDataAdapter.Dispose();
SQLDataAdapter = null;
}

public void FillsqlDataTable(ref DataTable oDatatable)
{
try
{
InitializeSQLCommand();
InitializeSQLDataAdapter();
SQLDataAdapter.Fill(oDatatable);
}
catch (System.Exception OleDBExceptionErr)
{
throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);
}
SQLCommand.Dispose();
SQLCommand = null;
SQLDataAdapter.Dispose();
SQLDataAdapter = null;

}

public int SQLExecuteStoredProcedure()
{
int functionReturnValue = 0;
try
{
SQLOpenConnection();
functionReturnValue = SQLCommand.ExecuteNonQuery();
}


catch (System.Exception OleDBExceptionErr)
{
throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);
}
finally
{
SQLCloseConnection();
}
return functionReturnValue;
}

public int SQLTLWDExecuteStoredProcedure()
{
int functionReturnValue = 0;
try
{

functionReturnValue = SQLCommand.ExecuteNonQuery();
}


catch (System.Exception OleDBExceptionErr)
{
throw new System.Exception(OleDBExceptionErr.Message, OleDBExceptionErr.InnerException);
}
return functionReturnValue;
}
#region " IDisposable Support "
// Visual Basic 添加此代码是为了正确实现可处置模式。
public void Dispose()
{
// 不要更改此代码。请将清理代码放入上面的 Dispose(ByVal disposing As Boolean) 中。
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion



}
...全文
53 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lizhimin0310 2008-11-03
  • 打赏
  • 举报
回复
d
jiang_jiajia10 2008-11-03
  • 打赏
  • 举报
回复
我放到我的项目里编译了一下没有报错
jiang_jiajia10 2008-11-03
  • 打赏
  • 举报
回复
有问题?
hanqingyu 2008-11-03
  • 打赏
  • 举报
回复
是userID不是user ID
zywinner168 2008-11-03
  • 打赏
  • 举报
回复
我记得好象是连接字符有问题,vb.net的和c#的有些不一样比如说,在vb.net里面“/”可以用,在c#里面就必须用“//”了,麻烦大家再帮忙看看啊,昨天用了下好象有点问题还用不了。

62,266

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧