2013-03-06 115 views
1

我正在看一个Web教程,教师讲的语言我不明白,视频也没有显示完整的长度。任何人都可以告诉我这条线应该看起来如何...无法将类型'字符串'隐式转换为'System.Data.CommandType'

private void Insertbtn_Click(object sender, EventArgs e) 
{ 
    OleDbCommand cmd = new OleDbCommand(); // this is good 
    cmd.CommandType = CommandType.Text; // this is good 
    cmd.CommandType = "INSERT INTO Students(StudentID, StudentName, StudentCNCI, 
    StudentDOB) Values('" + StudIDTxt.Text + "','" + StudNameTxt.Text + "','" + 
    StudCNCITxt.Text + "','" + StudDOBTxt.Text +")"; *// Need help here pls* 
    cmd.Connection=myCon; 
    myCon.Open(); 
    cmd.ExecuteNonQuery(); 
    myCon.Close(); 
} 

我正在开发VS 2010 C#。使用Access。

回答

6

你应该总是使用parameterized queries。您的代码已开放,可用于购买。

在您的查询,你应该使用CommandText属性,而不是CommandType

cmd.CommandText = "INSERT INTO Students(StudentID, StudentName, StudentCNCI, 
    StudentDOB) Values(@StudIDTxt, @StudNameTxt, @StudCNCITxt, @StudDOBTxt)"; 

cmd.Parameters.AddWithValue("@StudIDTxt", StudIDTxt.Text); 
cmd.Parameters.AddWithValue("@StudNameTxt", StudNameTxt.Text); 
cmd.Parameters.AddWithValue("@StudCNCITxt", StudCNCITxt.Text); 
cmd.Parameters.AddWithValue("@StudDOBTxtl", StudDOBTxt.Text); 
+1

与其指出错误,不如告诉他一个正确的版本。 – 2013-03-06 20:34:40

+0

谢谢Soner和Wiktor – bucketblast 2013-03-06 20:37:35

+2

现在它是更好的答案.. + 1为你@SonerGönül – Garry 2013-03-06 20:39:29

3

您:

cmd.CommandType = "INSERT INTO Students(StudentID, StudentName, StudentCNCI, 
    StudentDOB) Values('" + StudIDTxt.Text + "','" + StudNameTxt.Text + "','" + 
    StudCNCITxt.Text + "','" + StudDOBTxt.Text +")"; 

应该

cmd.CommandText = "INSERT INTO Students(StudentID, StudentName, StudentCNCI, 
    StudentDOB) Values('" + StudIDTxt.Text + "','" + StudNameTxt.Text + "','" + 
    StudCNCITxt.Text + "','" + StudDOBTxt.Text +"')"; 

你犯了一个错字。

此外,单引号丢失 - (StudDOBTxt.Text +")")应该是StudDOBTxt.Text +"')" - 这将导致SQL服务器端的语法错误。

至于你的查询的参数化形式(从SQL注入攻击是安全的形式),它将不得不使用问号而不是命名参数(这是它在ODBC中的工作方式,当命令类型是文本时),它将是是这样的:

cmd.CommandText = @"INSERT INTO Students(StudentID, StudentName, StudentCNCI, StudentDOB) 
         Values(?,?,?,?)"; 
    cmd.Parameters.Add(new OleDbParameter("p1", StudIDTxt.Text)); 
    cmd.Parameters.Add(new OleDbParameter("p2", StudNameTxt.Text)); 
    cmd.Parameters.Add(new OleDbParameter("p3", StudCNCITxt.Text)); 
    cmd.Parameters.Add(new OleDbParameter("p4", StudDOBTxt.Text)); 
+0

哦,是我现在已经看到了。非常感谢你,Ivan – bucketblast 2013-03-06 20:35:44

+0

@bucketblast不客气:)其他人指出,你应该考虑使用parametrized查询,他们阻止SQL注入(http://msdn.microsoft.com/en-us/library/system.data .odbc.odbccommand.parameters.aspx)。 – 2013-03-06 20:41:59

+0

这就是为什么我收到错误“查询表达式中的字符串中的语法错误''05/51/2013)'。”? – bucketblast 2013-03-06 20:52:41

2
private void Insertbtn_Click(object sender, EventArgs e) 
{ 
    OleDbCommand cmd = new OleDbCommand(); // this is good 
    cmd.CommandType = CommandType.Text; // this is good 
    cmd.CommandText = "INSERT INTO Students(StudentID, StudentName, StudentCNCI, 
    StudentDOB) Values('" + StudIDTxt.Text + "','" + StudNameTxt.Text + "','" + 
    StudCNCITxt.Text + "','" + StudDOBTxt.Text +")"; *// Need help here pls* 
    cmd.Connection=myCon; 
    myCon.Open(); 
    cmd.ExecuteNonQuery(); 
    myCon.Close(); 
    } 

它应该是命令文本不是命令类型

+0

Your Welcome @bucketblast – Garry 2013-03-06 20:37:58

相关问题