如何在c#窗体上的类中创建连接字符串和sql helper命令

时间:2013-07-11 23:06:07

标签: c# sql winforms

是否有人可以帮助我在c#windows窗体中的类代码中创建一个SqlHelper脚本。我想在同一个脚本中使用Sqlscalar和sqlNonquery以及sqlexecute,之后我希望命令在c#windows窗体中执行所请求的内容。

最好的问候 来自挪威的Jensen

1 个答案:

答案 0 :(得分:1)

这应该让你开始,这里是我写的一些代码,这里是一个插入

    public static void DbInsert(string table, string[] columnNames, string[] datatoadd)
{
    //string table name, column names to be updated, data as string in array
    //string[] strColumnNames = { "test1", "test2", "test3", "test4" };
    //string[] strValues = { "value1", "value2", "value3", "value4" };
    //Database.DbInsert("tblName", valscn, vals);


    SqlConnection sqlConn = new SqlConnection();
    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.CommandType = CommandType.Text;

    sqlConn.ConnectionString = ConnectionString("LiveConnection");
    string tblValues = "";
    string tblValuesCN = "";

    for (int i = 0; i < datatoadd.Length; i++)
    {
        sqlCmd.Parameters.Add(new SqlParameter("@" + i.ToString(), datatoadd[i].ToString()));
        tblValues = tblValues + "@" + i.ToString();
        if (i != datatoadd.Length-1)
        {
            tblValues = tblValues + ",";
        }
    }
    for (int i = 0; i < columnNames.Length; i++)
    {
        tblValuesCN = tblValuesCN + columnNames[i].ToString();
        if (i != columnNames.Length - 1)
        {
            tblValuesCN = tblValuesCN + ",";
        }
    }

    sqlCmd.CommandTimeout = 0;
    sqlCmd.CommandText = "Insert Into " + table + "(" + tblValuesCN + ") Values (" + tblValues + ")";

    sqlConn.Open();
    sqlCmd.Connection = sqlConn;
    sqlCmd.ExecuteNonQuery();


    sqlConn.Close();
    sqlCmd.Dispose();

}

以下是更新:

    public static void DbEdit(string table, string[] columnNames, string[] datatoadd, string whereclause)
{
    //string[] strColumnNames = { "Activate" };
    //string[] strData = { "Activated" };
    //Database.DbEdit("tblName", strColumnNames, strData, "id='" + activationresponse + "'");

    SqlConnection sqlConn = new SqlConnection();
    SqlCommand sqlCmd = new SqlCommand();
    sqlCmd.CommandType = CommandType.Text;

    sqlConn.ConnectionString = ConnectionString("test");
    string tblValues = "";
    string tblValuesCN = "";

    for (int i = 0; i < datatoadd.Length; i++)
    {
        sqlCmd.Parameters.Add(new SqlParameter("@" + i.ToString(), datatoadd[i].ToString()));
        tblValues = "@" + i.ToString();
        if (i != datatoadd.Length - 1)
        {

        }


        if (columnNames[i].ToString() != "")
        {
            tblValuesCN = tblValuesCN + columnNames[i].ToString() + "=" + tblValues + ",";
        }



    }
    tblValuesCN = tblValuesCN.Remove(tblValuesCN.Length - 1, 1);
    sqlCmd.CommandTimeout = 0;
    if (whereclause != "")
    {
        sqlCmd.CommandText = "Update " + table + " Set " + tblValuesCN + " where " + whereclause;
    }
    else
    {
        sqlCmd.CommandText = "Update " + table + " Set " + tblValuesCN;
    }
    sqlConn.Open();
    sqlCmd.Connection = sqlConn;
    sqlCmd.ExecuteNonQuery();


    sqlConn.Close();
    sqlCmd.Dispose();

}

您可以使用非查询或任何您想要的方式创建读取方法,但这两个方法将使您开始插入和更新,特别是刚刚发布的SINE链接,您可以从那里获取一些。