两个字符串有时C# [英] Two string sometime C#
问题描述
我有两个字符串
没有结果
我尝试过:
I have two string
No have result
What I have tried:
String 1 = "select id from dbo.normativi_lista where name="+ nametextbox.tex
String 2="select * from dbo.normativi_roba where id_fakture=" + String1
<pre>
PullData();
using (SqlConnection openCon = new SqlConnection(con))
{
String selectid = "select id from dbo.normativi_lista where naziv= " + nazivTextBox.Text;
String saveStaff = "SELECT id, redni_broj as 'R.b.', sifra as 'Šifra', naziv as 'Naziv', proizvodjac as 'Proizvođač', jedinica_mjere as 'J.M.',"
+ "kolicina as 'Količina', prosjecna_nab_cijena as 'Pr. Nab. Cij.', iznos as 'Iznos' from dbo.normativi_roba where id_fakture=" + selectid;
Console.WriteLine(saveStaff);
openCon.Open();
using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(saveStaff, con))
{
DataTable dt = new DataTable();
querySaveStaff.Fill(dt);
NormativRobaDataGridView.DataSource = dt;
NormativRobaDataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
NormativRobaDataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
NormativRobaDataGridView.Columns["id"].Visible = false;
NormativRobaDataGridView.Columns["R.b."].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
NormativRobaDataGridView.Columns["R.b."].Width = 80;
NormativRobaDataGridView.Columns["Šifra"].Width = 100;
NormativRobaDataGridView.Columns["Naziv"].MinimumWidth = 280;
NormativRobaDataGridView.Columns["Naziv"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
NormativRobaDataGridView.Columns["Proizvođač"].Width = 150;
NormativRobaDataGridView.Columns["J.M."].Width = 130;
NormativRobaDataGridView.Columns["Količina"].Width = 150;
NormativRobaDataGridView.Columns["Pr. Nab. Cij."].Width = 150;
NormativRobaDataGridView.Columns["Iznos"].Width = 150;
NormativRobaDataGridView.Update();
NormativRobaDataGridView.Refresh();
//countRowsLabel.Text = "Ukupno normativa: " + NormativListaDataGridView.RowCount.ToString();
//countRowsLabel2.Text = "Ukupno stavki: " + NormativRobaDataGridView.RowCount.ToString();
this.NormativRobaDataGridView.Sort(this.NormativRobaDataGridView.Columns["R.b."], ListSortDirection.Ascending);
//double[] columnData4 = new double[NormativRobaDataGridView.Rows.Count];
//columnData4 = (from DataGridViewRow row in NormativRobaDataGridView.Rows
// where row.Cells["Iznos"].FormattedValue.ToString() != string.Empty
// select Convert.ToDouble(row.Cells["Iznos"].FormattedValue)).ToArray();
//UkupnoLabel.Text = columnData4.Sum().ToString("#,0.00");
}
}
推荐答案
不要这样做!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。
连接字符串时会导致问题,因为SQL会收到如下命令:
Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.
When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
哪个SQL看作三个单独的命令:
Which SQL sees as three separate commands:
SELECT * FROM MyTable WHERE StreetAddress = 'x';
完全有效的SELECT
A perfectly valid SELECT
DROP TABLE MyTable;
完全有效的删除表格通讯和
A perfectly valid "delete the table" command
--'
其他一切都是评论。
所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。
所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?
当你在整个应用程序中修复它(并错过一个实例,有人会找到它)时,你会可能会发现您注意到的问题已经消失。
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.
So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
By the time you have fixed that throughout your app (and miss one instance and someone will find it) you will probably find the problem you noticed has gone.
除了SQL Injection漏洞之外,您永远不会执行第一个查询。最终会出现一个无效的SQL查询,如下所示:
In addition to the SQL Injection vulnerability, you never execute your first query. You will end up with an invalid SQL query which looks something like:
SELECT id, ... FROM dbo.normativi_roba where id_fakture=select id from dbo.normativi_lista where naziv= NULL; DELETE FROM dbo.normativi_roba; --Little Bobby Tables strikes again!
(用户输入的位置: NULL; DELETE FROM dbo.normativi_roba; - 小鲍比表再次敲击!)
相反,将两个查询连接在一起,并使用参数:
(Where the user typed in: "NULL; DELETE FROM dbo.normativi_roba; --Little Bobby Tables strikes again!")
Instead, join the two queries together, and use a parameter:
const string Query = @"SELECT
NR.id,
NR.redni_broj as 'R.b.',
NR.sifra as 'Šifra',
NR.naziv as 'Naziv',
NR.proizvodjac as 'Proizvođač',
NR.jedinica_mjere as 'J.M.',
NR.kolicina as 'Količina',
NR.prosjecna_nab_cijena as 'Pr. Nab. Cij.',
NR.iznos as 'Iznos'
FROM
dbo.normativi_roba As NR
INNER JOIN dbo.normativi_lista As NL
ON NL.id = NR.id_fakture
WHERE
NL.naviz = @naviz";
using (SqlConnection openCon = new SqlConnection(con))
using (SqlDataAdapter querySaveStaff = new SqlDataAdapter(Query, openCon))
{
querySaveStaff.SelectCommand.Parameters.AddWithValue("@naviz", nazivTextBox.Text);
DataTable dt = new DataTable();
querySaveStaff.Fill(dt);
...
这篇关于两个字符串有时C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!