Solved

C# Execute Query

Posted on 2009-05-02
3
5,397 Views
Last Modified: 2012-05-06
I am writing the code below to change passwords on instances of SQL Server. My first attempt, I tried to connect to the default instance by passing the computer name into the connection string:

SqlConnection conn = new SqlConnection("Data Source=" + computerName + ";Initial Catalog=master;User Id=sa;Password=;");.

However, I had an error during compile. Now I am trying by specifying localhost, but getting an "InvalidOperationException". Can someone look at my code and see what I am doing wrong?

-- PS The sa password is blank and the user names exist on the server.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Microsoft.Win32;
 
namespace RegistryTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] userNames = new string[] { "John", "Michael" };
 
            string computerName = System.Environment.MachineName;
            string password = "password";
 
            //SqlConnection conn = new SqlConnection("Data Source=" + computerName + ";Initial Catalog=master;User Id=sa;Password=;");
            SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=master;User Id=sa;Password=;");
 
            foreach (string userName in userNames)
            {
                SqlCommand cmd = new SqlCommand("ALTER LOGIN [" + userName + "] WITH PASSWORD=N'" + password + "', conn");
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
            }
 
            Console.WriteLine("FIN");
        }
    }
}

Open in new window

0
Comment
Question by:computerstreber
  • 2
3 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 24288915
this line is wrong:

                SqlCommand cmd = new SqlCommand("ALTER LOGIN [" + userName + "] WITH PASSWORD=N'" + password + "', conn");
                conn.Open();


must be:

                SqlCommand cmd = new SqlCommand("ALTER LOGIN [" + userName + "] WITH PASSWORD=N'" + password + "' ", conn);
                conn.Open();
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 24288916
explanation: in your code, the ",conn" was part of the connection string, instead of specifying the second parameter for the new SqlCommand,..
0
 
LVL 3

Author Closing Comment

by:computerstreber
ID: 31577291
Awesome... that worked beautifully....
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now