我有一小段代码,它最初反复创建了一个SqlDataAdapter对象.
尝试简化我的调用,我用SqlCommand替换了SqlDataAdapter,并将SqlConnection移到了循环之外.
现在,每当我尝试编辑返回到我的DataTable的数据行时,我都会抛出之前未抛出的ReadOnlyException.
注意:我有一个自定义函数,可根据其ID检索员工的全名.为简单起见,我在下面的示例代码中使用了"John Doe"来证明我的观点.
ExampleQueryOld与SqlDataAdapter一起使用 ; 每当我尝试写入DataRow的元素时,ExampleQueryNew都会因ReadOnlyException 而失败:
这有效,没有问题:
public static DataTable ExampleQueryOld(string targetItem, string[] sqlQueryStrings) {
DataTable bigTable = new DataTable();
for (int i = 0; i < sqlQueryStrings.Length; i++) {
string sqlText = sqlQueryStrings[i];
DataTable data = new DataTable(targetItem);
using (SqlDataAdapter da = new SqlDataAdapter(sqlText, Global.Data.Connection)) {
try {
da.Fill(data);
} catch (Exception err) {
Global.LogError(_CODEFILE, …Run Code Online (Sandbox Code Playgroud) 如何将Parameters.AddWithValue与SqlDataAdapter一起使用.下面搜索代码.
var da = new SqlDataAdapter("SELECT * FROM annotations WHERE annotation LIKE '%"+txtSearch.Text+"%'", _mssqlCon.connection);
var dt = new DataTable();
da.Fill(dt);
Run Code Online (Sandbox Code Playgroud)
我重写了这样的代码:
SqlDataAdapter da;
da = new SqlDataAdapter("SELECT * FROM annotations WHERE annotation LIKE '%@search%'", _mssqlCon.connection);
da.SelectCommand.Parameters.AddWithValue("@search",txtSearch.Text);
var dt = new DataTable();
da.Fill(dt);
Run Code Online (Sandbox Code Playgroud)
但它失败了.
我想知道哪一个具有更好的返回性能DataTable.SqlDataReader我在这里使用DataTable.Load(dr)
使用SqlDataReader:
public static DataTable populateUsingDataReader(string myQuery)
{
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(constring))
{
SqlCommand cmd = new SqlCommand(myQuery, con);
con.Open();
SqlDataReader dr = null;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows)
{
dt.Load(dr);
}
return dt;
}
}
Run Code Online (Sandbox Code Playgroud)
使用SqlDataAdapter:
public DataTable populateUsingDataAdapter(string myQuery)
{
SqlDataAdapter dap = new SqlDataAdapter(myQuery,cn);
DataSet ds = new DataSet();
dap.Fill(ds);
return ds.Tables[0];
}
Run Code Online (Sandbox Code Playgroud) 我对连接模型和实体框架中的断开连接很困惑.
我使用的是传统的ADO.net(DataReader用于连接模型和DataAdapter断开连接的模型),当我需要发送更多用户需要更新或插入以及在几种情况下断开模型时,我知道我使用连接模型数据到其他进程对内存中的数据进行一些操作并将它们发送回数据库.
现在我在EF中阅读了一些关于连接模型和断开连接模型的文章,我很困惑为什么要将实体明确地附加到断开连接模型中的上下文?我还读过web中的默认行为是断开连接的模型,而WPF中的默认行为是连接模型!
asp.net ado.net entity-framework sqldatareader sqldataadapter
每个人我都是学生,不熟悉.NET,特别是MVC3开发,但对于我的一个项目,我要研究它,因此经历学习阶段我面临的问题和混乱是关于数据库连接,我看到了什么.关于从数据库中检索记录是这样的:
//Method One:
var conn = new SqlConnection(conString.ConnectionString);
const string cmdString = "Select * FROM table";
var cmd = new SqlCommand(cmdString, conn);
var mySqlDataAdapter = new SqlDataAdapter(cmd);
mySqlDataAdapter = new SqlDataAdapter(cmd);
mySqlDataAdapter.Fill(myDataSet, "design");
// making a new SqlCommand object with stringQuery and SqlConnection object THEN a new SqlDataAdapter object with SqlCommand object and THEN filling up the table with the resulting dataset.
Run Code Online (Sandbox Code Playgroud)
但是当我查看MSDN Library时,我发现SqlDataAdapter提供了一个构造函数SqlDataAdapter(String,String),它直接接受一个SelectCommand和一个连接字符串来启动,从而跳过SqlCommand之间的角色,如下所示:
//Method Two:
var conn = new SqlConnection(conString.ConnectionString);
const string cmdString = "Select * FROM …Run Code Online (Sandbox Code Playgroud) Dim dt As New DataTable
Dim da As New SqlDataAdapter(s, c)
c.Open()
If Not IsNothing(da) Then
da.Fill(dt)
dt.Select("GroupingID = 0")
End If
GridView1.DataSource = dt
GridView1.DataBind()
c.Close()
Run Code Online (Sandbox Code Playgroud)
当我调用da.fill时,我将从查询中插入所有记录.我当时希望过滤它们以仅显示GroupingID等于0的那些.当我运行上面的代码时.我收到了所有数据,过滤器无法正常工作.请问您能告诉我如何使其正常工作.谢谢.
我正在使用以类似于以下代码的布局编写的DAL对象.我简化了很多代码只是为了显示设置.
public class UserDatabase : IDisposable
{
private SqlDataAdapter UserDbAdapter;
private SqlCommand UserSelectCommand;
private SqlCommand UserInsertCommand;
private SqlCommand UserUpdateCommand;
private SqlCommand UserDeleteCommand;
private System.Data.SqlClient.SqlConnection SQLConnection;
public UserDatabase()
{
this.SQLConnection = new System.Data.SqlClient.SqlConnection(ConnectionString);
this.UserDbAdapter= new SqlDataAdapter();
this.UserDbAdapter.DeleteCommand = this.UserDeleteCommand;
this.UserDbAdapter.InsertCommand = this.UserInsertCommand;
this.UserDbAdapter.SelectCommand = this.UserSelectCommand;
this.UserDbAdapter.UpdateCommand = this.UserUpdateCommand;
}
private bool FillUsers(DataSet UserDataSet, out int numberOfRecords)
{
bool success = true;
numberOfRecords = 0;
string errorMsg = null;
this.UserDbAdapter.SelectCommand = this.GetUsersSelectCommand();
numberOfRecords = UserDbAdapter.Fill(UserDataSet, UsersTableName);
return success;
}
private SqlCommand GetUserSelectCommand() …Run Code Online (Sandbox Code Playgroud) 我正在尝试将参数添加到sqlDataAdapter.我试过使用parameters.add()但是适配器不是sqlCommand.这是我的一些代码.
Private Sub convertToCSV(ByVal SqlQuery As String)
Dim Dt As New DataTable()
Dim SqlCon As New SqlConnection("Data Source=db;Initial Catalog=productionservicereminder;User Id=id;Password=pass;")
Dim Ada As New SqlDataAdapter(SqlQuery, SqlCon)
Ada.Fill(Dt)
Public Sub excSP(ByVal ReprtID As Integer, ByVal pgid As Integer)
convertToCSV(sql4)
End Sub
Run Code Online (Sandbox Code Playgroud)
基本上我想尝试做这样的事情:
Ada.Parameters.Add(New SqlParameter("@pgid", pgid))
Run Code Online (Sandbox Code Playgroud) 我在MS SQL 2005中有一个带有primarykey的表,它有几十万个记录.当我在Management studio中查询它以获取记录时,它会带来非常快的但当我使用下面的代码找到它时,它需要很多秒.我必须使用数据集,因为我需要更新行.我该如何改善表现?
objData . ProcName ="myProcName"
objData . CreateCommand()
objData . Parameters("@BName", SqlDbType. VarChar, 20, "MyBranch1")
SqlDataAdapter da = objData . createAdapter()
da . Fill(ds,"MyTable1")
Run Code Online (Sandbox Code Playgroud)
虽然proc代码非常简单:
select * from MyTable1 Where BranchName = @BName
Run Code Online (Sandbox Code Playgroud)
这个数据集将以相同的方式打开5个表,因此总时间超过一分钟
这个问题似乎很常见,我已经完成了这个答案.
不幸的是,我的页面仍然没有被分页.这是我的代码在C#中的样子:
SqlCommand command = new SqlCommand("(SELECT ......", Connection);
SqlDataAdapter myAdapter = new SqlDataAdapter(command);
DataTable dt = new DataTable();
myAdapter.Fill(dt);
command.Connection = connection;
command.Connection.Open();
GridView1.DataSource = dt;
GridView1.DataBind();
GridView1.AllowPaging = true;
GridView1.PageSize = 15;
command.Connection.Close();
command.Connection.Dispose();
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我这样做时,我的分页没有显示出来.难道我做错了什么?
谢谢
sqldataadapter ×10
c# ×6
ado.net ×3
asp.net ×3
sqlcommand ×3
.net ×2
datatable ×2
parameters ×2
performance ×2
vb.net ×2
filter ×1
gridview ×1