1

Is it anyone out there that can help me make a SqlHelper script in class code in a c# windows form. I want Sqlscalar and sqlNonquery and sqlexecute in the same script and after that I want the command to do the requested things in c# windows form.

With Best Regard Jensen from Norway

2

1 Answer 1

1

This should get you started, here is some code I wrote, here here an Insert

    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();

}

Here is an update:

    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();

}

You can make a read method using nonquery or whatever you want, but these two will get you started with inserting and updating, especially with the link SINE just posted, you can take some from there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.