Solved

C# Execute Query

Posted on 2009-05-02
3
5,406 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 143

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 143

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 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