Error when trying to connect a web service with a database

I get this error(s):

error CS1502: The best overloaded method match for 'System.Data.SqlClient.SqlConnection.SqlConnection(string, System.Data.SqlClient.SqlCredential)' has some invalid arguments

error CS1503: Argument 2 : cannot convert from 'System.Data.SqlClient.SqlConnection' to 'System.Web.WebPages.HelperResult'

This is my code:

public string NumeroFactura(string NoDeFactura)
{
        string DtsConnection = "ITLIGENCIA-POSSQLEXPRESS; Initial Catalog = SSOLINVBASESQL; Trusted_Connection = Yes";
        SqlConnection Con = new SqlConnection(DtsConnection);
        Con.Open();

        SqlDataAdapter CMD = new SqlConnection("select * from INVE_FACTURAS_PRODUCTOS_ENC where NFACTURA='"+ NoDeFactura+ "'",Con);
        DataSet DS = new DataSet();
        CMD.Fill(DS, "DATOS");

        DataTable TableNFACTURA = DS.Tables[0];
        string NFACTURA = TableNFACTURA.Rows[0]["NFACTURA"].ToString();

        return "El número de factura es" + NFACTURA;
}

SqlDataAdapter CMD = new SqlConnection(...

should be

SqlDataAdapter CMD = new SqlDataAdapter(...

Also, look up "SQL injection" and "parameterized queries", you're doing something pretty dangerous.

While I'm at it, using select * (and SqlDataAdapter for that matter) to retrieve one row and one column isn't terribly efficient. And you're assuming that you will get a row back; if NoDeFactura doesn't exist in the database, TableNFACTURA.Rows[0]["NFACTURA"].ToString() will throw a null reference exception.


You are missing Data Source or Server key. Your connection string should look like

Server=ITLIGENCIA-POSSQLEXPRESS; Initial Catalog = SSOLINVBASESQL; Trusted_Connection = Yes
链接地址: http://www.djcxy.com/p/89842.html

上一篇: 汇编语言寻址模式(IA

下一篇: 尝试将Web服务连接到数据库时出错