Lỗi data type mismatch in criteria expression trong c năm 2024

  1. Do yourself a favour, and stop using Visual Studio default names for everything - you may remember that "TextBox9" is the users first name today, but when you have to modify it in three weeks time, will you then? Use descriptive names - "tbFirstName" for example - and your code becomes easier to read, more self documenting, easier to maintain - and surprisingly quicker to code because Intellisense can get to to "tbFirstName" in three keystrokes, where "TextBox9" takes thinking about and 8 keystrokes...
  1. 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'

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;

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

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?

  1. ---
  2. >
    Lỗi data type mismatch in criteria expression trong c năm 2024
    Gửi bởi natsu1
    mình sai o chổ câu truy vấn mình truyền vào thì nó hiểu là chuổi chứ không phải kiểu int giup mình sửa chổ này với

Để nó hiểu kiểu int thì bạn bỏ nháy '

Mã:

string strSQL = "select from Nha where ID=" + id;

Bạn đã lấy thông tin dựa vào id, sao ở dưới lại kiểm tra một lần nữa làm gì vậy ?


  1. >
    Lỗi data type mismatch in criteria expression trong c năm 2024
    Gửi bởi trungvx

Để nó hiểu kiểu int thì bạn bỏ nháy '

Mã:

string strSQL = "select
from Nha where ID=" + id;

Bạn đã lấy thông tin dựa vào id, sao ở dưới lại kiểm tra một lần nữa làm gì vậy ?

"Bạn đã lấy thông tin dựa vào id, sao ở dưới lại kiểm tra một lần nữa làm gì vậy ?" là lúc đầu mình chưa có để dk where" mình đã làm được cảm ơn bạn nhìu

-
  1. > sẵn tiện cho mình hỏi muốn viết câu truy vấn lấy 1 khoảng giá trị thì mình viết between "kieu int" and "kieu int" thì mình viết sao? -
  2. > [IMG]images/smilies/1.gif[/IMG] Between ko có vẫn dùng bình thường mà, tách làm 2 cái and là được. ... where v1<=v and v<=v2... -
  3. > bạn có thể viết 1 câu dùm mình được không? -
  4. lỗi Data type mismatch in criteria expression trong luc select dữ liệu

    các bạn giúp mình với code lấy dữ liệu của mình dựa trên ID

Mã:

public static DataTable SelectAllNha(int id) > { > DataTable dt = null; > // > tao ra cau truy van select > string strSQL = "select * from Nha where ID='"+id+"' ";// chổ này khi mình để dk where vào thì nó sẽ báo lỗi như hình > // > co lop ho truc thuc thi cau truy nay > tra gia tri >

DataProvider dp = new DataProvider(); > dt = dp.ExecuteQuery(strSQL); > for (int i = 0; i < dt.Rows.Count; i++) > if (id == int.Parse(dt.Rows[i]["ID"].ToString())) > return dt; > return dt; > }

Mã:

public DataTable ExecuteQuery(string _strSQL) > { > DataTable dt = new DataTable(); > OleDbDataAdapter adapter = new OleDbDataAdapter(_strSQL, strConnect); > adapter.Fill(dt); > return dt; > }

-
  1. > [IMG]images/smilies/daydreaming.gif[/IMG] Mình ko hiểu sao bạn chạy cái vòng lặp để làm gì? select theo ID thì chỉ có 1 hoặc ko có gì, đâu ra nhiều mà phải lặp. với lại, cứ cho là có nhiều đi chăng nữa thì cuối cùng bạn cũng return dt=> khác gì nhau vậy? -
  2. > [IMG]images/smilies/biggrin.png[/IMG]t viết cho bạn rồi còn gì.hix -
  3. >
    Lỗi data type mismatch in criteria expression trong c năm 2024
    Gửi bởi trungvx

Để nó hiểu kiểu int thì bạn bỏ nháy '

Mã:

string strSQL = "select * from Nha where ID=" + id;

Bạn đã lấy thông tin dựa vào id, sao ở dưới lại kiểm tra một lần nữa làm gì vậy ?

cám ơn bạn rất nhiều! chỉ có mỗi dấu nháy đơn hay kép mà ngồi mò debug cả ngày không biết vì đâu, may mà đọc được bài của bạn [IMG]images/smilies/smile.png[/IMG] thanks ---

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  • BB code đang Bật
  • Smilies đang Bật
  • code đang Bật
  • code is Bật
  • HTML code đang Tắt

Nội quy - Quy định