我有一个带有许多方法的dal层,它们都调用存储过程,一些返回列表(所以使用SqlDataReader),其他只有特定值.
我有一个帮助方法,创建SqlCommand:
protected SqlCommand CreateSprocCommand(string name, bool includeReturn, SqlDbType returnType)
{
SqlConnection con = new SqlConnection(this.ConnectionString);
SqlCommand com = new SqlCommand(name, con);
com.CommandType = System.Data.CommandType.StoredProcedure;
if (includeReturn)
com.Parameters.Add("ReturnValue", returnType).Direction = ParameterDirection.ReturnValue;
return com;
}
Run Code Online (Sandbox Code Playgroud)
现在我的平均(过度简化)方法体看起来像:
SqlCommand cmd = CreateSprocCommand("SomeSprocName"); //an override of the above mentioned method
try {
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader()) {
//some code looping over the recors
}
//some more code to return whatever needs to be returned
}
finally {
cmd.Connection.Dispose(); …Run Code Online (Sandbox Code Playgroud) 我有这个代码:
public void chyt_data()
{
try
{
SqlCommand novyprikaz = new SqlCommand("SELECT * FROM zajezd WHERE akce=" + currentrowstring, spojeni);
spojeni.Open();
SqlDataReader precti = novyprikaz.ExecuteReader();
if (precti.Read())
{
zakce.Text = precti.GetString(0);
zname.Text = precti.GetString(2);
}
}
catch (Exception ex)
{
MessageBox.Show("Chybové hlášení2: " + ex.Message.ToString());
}
spojeni.Close();
}
Run Code Online (Sandbox Code Playgroud)
如果我在其中插入列名称,它是这样的:
zakce.Text = precti.GetString("akce");
Run Code Online (Sandbox Code Playgroud)
它不会起作用.
愿有人请帮我解决这个问题吗?非常感谢
它给出了两个错误:
1:错误1'System.Data.Common.DbDataReader.GetString(int)'的最佳重载方法匹配有一些无效的参数
2:错误2参数1:无法从'string'转换为'int'
我在下面获得了以下代码:
Select column odjezd
From table stanice
Order by odjezd
where akce=zakce.Text
SqlCommand combobox = new SqlCommand("SELECT odjezd FROM stanice
ORDER BY odjezd ASC
WHERE akce="+zakce.Text, spojeni);
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误:附近的语法不正确WHERE。
有人可以改善我的代码吗?提前致谢
好吧,这是微不足道的,但它一直困扰着我.我有这样的代码
oCmd = new SqlCommand("getBooking", oCon);
oCmd.CommandType = CommandType.StoredProcedure;
oRs = oCmd.ExecuteReader();
do while (oRs.Read()) {
// do stuff
}
oRs.Close();
oCmd.Dispose();
Run Code Online (Sandbox Code Playgroud)
但是我Dispose可以在ExecuteReader这之后移动到:
oCmd = new SqlCommand("getBooking", oCon);
oCmd.CommandType = CommandType.StoredProcedure;
oRs = oCmd.ExecuteReader();
oCmd.Dispose();
do while (oRs.Read()) {
// do stuff
}
oRs.Close();
Run Code Online (Sandbox Code Playgroud)
我试过了,它有效,但感觉就像我在调皮.这种方法有问题吗?我问,因为经常需要重新使用其中的SqlCommand对象do while,而不是我不想创建多个SqlCommand对象.
con.Open();
string qry="insert into reg1 values ('"+txtname.Text+"','"+txtaddress.Text+"','"+txtpin.Text+"','"+txtage.Text+"','"+txtgender.Text+"','"+txtcourse.Text+"','"+txtcollege.Text+"','"+txtfname.Text+"','"+txtoccup.Text+"','"+txtmname.Text+"','"+txtskills.Text+"','"+txtmobile.Text+"','"+txtemail.Text+"')";
SqlCommand cmd = new SqlCommand(qry, con);
cmd.ExecuteNonQuery();
con.Close();
Run Code Online (Sandbox Code Playgroud)
我尝试执行此代码时收到以下错误消息:
列名或提供的值数与表定义不匹配.
任何人都可以帮我找到错误吗?
我遇到了一些麻烦.我在一个使用块中有一个SqlCommand.通常这很好.但是,这次代码退出使用块,但命令没有被释放.这让我很难过.
using (SqlCommand transferCommand = new SqlCommand(strHeaderStoredProcedure, connection))
{
transferCommand.CommandType = System.Data.CommandType.StoredProcedure;
transferCommand.Parameters.Add("@InvtTransferID", SqlDbType.UniqueIdentifier).Value = InvtTransferID;
SqlDataReader transferReader = transferCommand.ExecuteReader();
while (transferReader.Read())
{
//do my stuff, this all works fine
}
}
using (SqlCommand transferCommand = new SqlCommand(strDetailsStoredProcedure, connection))
{
transferCommand.CommandType = System.Data.CommandType.StoredProcedure;
transferCommand.Parameters.Add("@InvtTransferID", SqlDbType.UniqueIdentifier).Value = InvtTransferID;
SqlDataReader transferReader = transferCommand.ExecuteReader(); <-- Error happens here.
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试重用我的连接来运行一堆存储过程.这是最奇怪的事情.我在另一个几乎相同的表单上有一些代码,它运行正常,处理命令并允许我创建另一个.我有什么想法我做错了吗?(我正试图让这段代码在帖子中看起来不错,但它似乎超出了我的微薄力量...对于凌乱的代码感到抱歉.)
错误是:已经有一个与此命令关联的打开DataReader,必须先关闭它.
我正在c#winforms中创建用户登录屏幕,我希望能够根据此链接检查用户的用户名和密码以防止SQL数据库中的记录,但是我的代码会抛出一个异常,说"用户附近的语法不正确".
任何人都可以帮我弄清楚我的代码有什么问题吗?违规代码如下.
private bool CompareStrings(string string1, string string2)
{
return String.Compare(string1, string2, true, System.Globalization.CultureInfo.InvariantCulture) == 0 ? true : false;
}
private void LoginBtn_Click(object sender, EventArgs e)
{
//var username = textBox1.Text;
//var password = maskedTextBox1.Text;
try
{
SqlConnection Conn = new SqlConnection("Data Source=***********;Initial Catalog=*********;Persist Security Info=True;User ID=*********;Password=*******");
SqlCommand com = new SqlCommand();
com.Connection = Conn;
Conn.Open();
com.CommandText = ("SELECT (Username) AS User, (Password) as Pass FROM dbname WHERE User='" + textBox1.Text + "'");
SqlDataReader reader …Run Code Online (Sandbox Code Playgroud) 我正在尝试从给定的表生成随机ID.我可以看到调试中生成的随机数,但是当我到达reader.Read()行时,它显示Enumeration没有产生任何结果.
我无法得到我所缺少的东西.
private static void GetRandomId(int maxValue)
{
string connectionString =
"Data Source=local;Initial Catalog=Test;user id=Test;password=Test123;";
string queryString = @"SELECT TOP 1 Id from Pointer WHERE Id > (RAND() * @max);";
using (var connection = new SqlConnection(connectionString))
{
var command = new SqlCommand(queryString, connection);
command.Parameters.AddWithValue("@max", maxValue);
connection.Open();
using (var reader = command.ExecuteReader()) <-- // Here I can see the randon value generated
{
while (reader.Read())
{
//Here reader shows : Enumeration yielded no results
Console.WriteLine("Value", reader[1]);
reader.Close();
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个SqlDataReader,需要从我的数据库中读取某些值.在SqlCommand其中选择这些值是这样的:
SqlCommand myCommand = new SqlCommand("SELECT * FROM dbo.Confronting_Value", valueConnection);
Run Code Online (Sandbox Code Playgroud)
数据库中的每个条目都包含"攻击者","后卫"和"价值".所有3都包含整数值.
例如
Attacker: "665", Defender: "443", Value: "3".
Run Code Online (Sandbox Code Playgroud)
可能存在多个条目,其中"攻击者"具有值"665".
现在,SELECT WHERE Attacker = 665很简单,但我有一个变量Black.ID.我想选择攻击者与Black.ID具有相同值的所有条目.我怎么做?
我试图找到它.但我找不到我的答案.所以我决定问这个问题.我需要你的帮助.
我希望在不覆盖Debit,Score列的情况下将值添加到表值中.它会增加当前价值.
cmd = new SqlCommand("UPDATE Users SET Debit=@debit,
Score=@score
WHERE Phone=@phone", con);
con.Open();
cmd.Parameters.AddWithValue("@phone", textBox1.Text);
cmd.Parameters.AddWithValue("@debit", textBox2.Text);
cmd.Parameters.AddWithValue("@score", textBox3.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("?????????");
con.Close();
Run Code Online (Sandbox Code Playgroud)
例如:
Table, Phone: 999 | Debit: 1500 | Score: 100 //current <br>
Run Code Online (Sandbox Code Playgroud)
当我从textBox1 = 999,textBox2 = 500,textBox3 = 50添加值时
Table, Phone: 999, Debit: 2000, Score: 150 //updating like that
Run Code Online (Sandbox Code Playgroud)
我知道这样的SQL查询.但我不知道如何编写代码SqlCommand
UPDATE Users
SET Debit = Debit + [user input], Score = Score + [user input]
WHERE = Phone
Run Code Online (Sandbox Code Playgroud)
有什么建议?
(抱歉我的英语很糟糕,我希望你们明白我要问的是什么)
谢谢
c# ×10
sqlcommand ×10
sql ×4
sql-server ×3
dispose ×2
ado.net ×1
asp.net ×1
getstring ×1
using ×1